要求

1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作

  1. def find(backend):
  2. '''
  3. 查看backend下sever信息
  4. :param backend:
  5. :return:
  6. '''
  7. ls = []
  8. with open('HA.txt', 'r') as f:
  9. T = True
  10. for line in f:
  11. # 找到第一个backend,做记录 T = False
  12. if line.strip() == 'backend %s' % (backend):
  13. T = False
  14. continue
  15. # 找到第二个backend,做记录 T = True
  16. if T == False and line.strip().startswith('backend'):
  17. T = True
  18. break
  19. # 把关于False的加入到列表中
  20. if T == False and line.strip():
  21. ls.append(line.strip())
  22. return ls
  23.  
  24. def check_bk():
  25. '''
  26. 列出backend的信息
  27. :return: 选中的backend
  28. '''
  29. ls = []
  30. T = True
  31. with open('HA.txt', 'r') as f1:
  32. for line in f1:
  33. if line.strip().startswith('backend') == True:
  34. to = line.split(' ')
  35. ls.append(to[1])
  36. for i, v in enumerate(ls,1):
  37. print (i, v)
  38. while T:
  39. t1 = input('请输入查看的server的 backend信息')
  40. if t1.isdecimal() and int(t1) in range(0,len(ls)+1):
  41. a = ls[int(t1)-1]
  42. break
  43. else:
  44. print('输入有误,重新输入')
  45. return a
  46.  
  47. def backup():
  48. '''
  49. 备份
  50. :return:
  51. '''
  52. with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2:
  53. for line in f1:
  54. f2.write(line)
  55.  
  56. def add(bk, sr):
  57. '''
  58. 添加记录
  59. :param bk:
  60. :param sr:
  61. :return:
  62. '''
  63. r_list = find(bk)
  64. # 没有关于bk的记录,直接添加内容
  65. T = True
  66. a = 'backend %s' % bk
  67. backup()
  68. if not r_list:
  69. with open('HA.txt', 'a') as f1:
  70. f1.write('\n%s\n'% a)
  71. f1.write(" %s\n" % sr)
  72. # 有关于bk的记录
  73. else:
  74. # 没有sever记录
  75. backup()
  76. if sr not in r_list:
  77. with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
  78. for line in f1:
  79. if line.strip() == 'backend %s' % (bk):
  80. T = False
  81. f2.write(line)
  82. continue
  83. if T == False and line.strip().startswith('backend'):
  84. T = True
  85. if T == True:
  86. f2.write(line)
  87. else:
  88. if sr not in r_list:
  89. r_list.append(sr)
  90. t = '\n '.join(r_list)
  91. f2.write(' '* 8 + t + '\n\n')
  92. # 有sever记录,直接复制
  93. else:
  94. print('记录已存在,请重新选择')
  95. caidan()
  96.  
  97. def update_bk ():
  98. '''
  99. 需要修改或删除backend
  100. :return:
  101. '''
  102. backup()
  103. T = True
  104. bk = check_bk()
  105. ls =[]
  106. with open('HA.txt', 'r') as f1:
  107. for line in f1:
  108. if line.strip().startswith('backend') == True:
  109. to = line.split(' ')
  110. ls.append(to[1].strip())
  111. c = input('''
  112. 请选择需要选项:
  113. 1 修改backend
  114. 2 删除backend
  115. ''')
  116. while T:
  117. #修改backend信息
  118. if c == '':
  119. bk1 = input('请输入修改后的backend信息')
  120. t3 = input('信息确认请按 y, 重新输入请按其他键')
  121. if bk1 in ls:
  122. print('信息已存在,请重新选择')
  123. break
  124. else:
  125. if t3.lower() == 'y':
  126. with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
  127. for line in f1:
  128. #需要修改的backend
  129. if line.strip() == 'backend %s' % (bk.strip()):
  130. f2.write('backend %s \n' % (bk1))
  131. else:
  132. f2.write(line)
  133. print('修改成功')
  134. break
  135. else:
  136. continue
  137. #删除backend信息
  138. elif c == '':
  139. t = find(bk.strip())
  140. while T:
  141. with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
  142. for line in f1:
  143. if line.strip() == 'backend %s' % (bk.strip()):
  144. continue
  145. if line.strip() in t:
  146. continue
  147. else:
  148. f2.write(line)
  149. print('删除成功')
  150. break
  151. break
  152.  
  153. def check_sr():
  154. T1 = True
  155. while T1:
  156. cont = 0
  157. a1 = input('sever:')
  158. ti = a1.split('.')
  159. for ii in ti:
  160. if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
  161. cont = cont + 1
  162. continue
  163. else:
  164. print('输入有误,请输入由四组0-255组成的IP地址')
  165. break
  166. if cont == 4:
  167. break
  168. return a1
  169.  
  170. def update_sr():
  171. '''
  172. 修改或删除sever
  173. :return:
  174. '''
  175. t = check_bk()
  176. backup()
  177. T = True
  178. T1 = True
  179. ls = find(t.strip())
  180. cc = 0
  181. with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
  182. for line in f1:
  183. if line.strip() == 'backend %s' %(t.strip()):
  184. T = False
  185. f2.write(line)
  186. continue
  187. if T == False and line.strip().startswith('backend'):
  188. T = True
  189. if T == True:
  190. if line.strip() != cc and line.strip() not in ls:
  191. f2.write(line)
  192.  
  193. else:
  194. for i, v in enumerate(ls, 1):
  195. print ('%s: %s' % (i, v))
  196. while T1:
  197. v1 = input('请输入要修改或删除的编号')
  198. if v1.isdecimal() and int(v1) in range(len(ls)+1):
  199. break
  200. else:
  201. print('输入有误,请重新输入')
  202. continue
  203. x = input('请选择 1 = 修改 其他键 = 删除')
  204. if x == '':
  205. while T1:
  206. a1 = check_sr()
  207. a2 = input('weight:')
  208. a3 = input('maxconn:')
  209. if a2.isdecimal() and a3.isdecimal():
  210. t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
  211. print('backend: %s \n%s' % (t, t1))
  212. else:
  213. print('所输入weight 和 maxconn 必须为数字,请重新输入')
  214. continue
  215. li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
  216. # server
  217. if li_sr in ls:
  218. tt = input('server已存在,请按 y = 重新输入,或按 other key = 返回')
  219. if tt == 'y':
  220. continue
  221. else:
  222. break
  223. else:
  224. a4 = input('请确认信息正确,y = 确认, other key = 重新输入')
  225.  
  226. if a4 == 'y':
  227. f2.write(' '*8 + li_sr +'\n')
  228. else:
  229. continue
  230. print('修改成功')
  231. break
  232. cc = ls[int(v1)-1]
  233. del ls[int(v1)-1]
  234. for tt in ls:
  235. f2.write(' '*8 + tt +'\n')
  236. T = True
  237. else:
  238. print('删除成功')
  239. cc = ls[int(v1)-1]
  240. del ls[int(v1)-1]
  241. for tt in ls:
  242. f2.write(' '*8 + tt +'\n')
  243. T = True
  244.  
  245. def caidan ():
  246. T = True
  247. while T:
  248. ls = input('''请输入所需编号:
  249. 1 查看backend下sever信息
  250. 2 添加记录
  251. 3 修改或删除backend
  252. 4 修改或删除sever
  253. 5 退出
  254. ''')
  255. if ls == '':
  256. i = check_bk()
  257. l1 = find(i.strip())
  258. for a in l1:
  259. print (a)
  260. elif ls == '':
  261. while T:
  262. t = input('请输入backend信息')
  263. print('请输入sever信息')
  264. cont = 0
  265. while T:
  266. a1 = input('sever:')
  267. ti = a1.split('.')
  268. for ii in ti:
  269. if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4:
  270. cont = cont + 1
  271. continue
  272. else:
  273. print('输入有误,请输入由四组0-255组成的IP地址')
  274. break
  275. if cont == 4:
  276. break
  277. while T:
  278. a2 = input('weight:')
  279. a3 = input('maxconn:')
  280. if a2.isdecimal() and a3.isdecimal():
  281. t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
  282. print('backend: %s \n%s' % (t, t1))
  283. break
  284. else:
  285. print('所输入weight 和 maxconn 必须为数字,请重新输入')
  286. continue
  287. t3 = input('信息确认请按 y, 重新输入请按其他键')
  288. if t3.lower() == 'y':
  289. add (t, t1)
  290. print('添加成功')
  291. break
  292. else:
  293. continue
  294. elif ls == '':
  295. update_bk()
  296. elif ls == '':
  297. update_sr()
  298. elif ls == '':
  299. exit()
  300. else:
  301. print('输入有误,请重新输入')
  302.  
  303. caidan()
  1. def find(backend):
    '''
    查看backend下sever信息
    :param backend:
    :return:
    '''
    ls = []
    with open('HA.txt', 'r') as f:
    T = True
    for line in f:
    # 找到第一个backend,做记录 T = False
    if line.strip() == 'backend %s' % (backend):
    T = False
    continue
    # 找到第二个backend,做记录 T = True
    if T == False and line.strip().startswith('backend'):
    T = True
    break
    # 把关于False的加入到列表中
    if T == False and line.strip():
    ls.append(line.strip())
    return ls
  2.  
  3. def check_bk():
    '''
    列出backend的信息
    :return: 选中的backend
    '''
    ls = []
    T = True
    with open('HA.txt', 'r') as f1:
    for line in f1:
    if line.strip().startswith('backend') == True:
    to = line.split(' ')
    ls.append(to[])
    for i, v in enumerate(ls,):
    print (i, v)
    while T:
    t1 = input('请输入查看的server的 backend信息')
    if t1.isdecimal() and int(t1) in range(,len(ls)+):
    a = ls[int(t1)-]
    break
    else:
    print('输入有误,重新输入')
    return a
  4.  
  5. def backup():
    '''
    备份
    :return:
    '''
    with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2:
    for line in f1:
    f2.write(line)
  6.  
  7. def add(bk, sr):
    '''
    添加记录
    :param bk:
    :param sr:
    :return:
    '''
    r_list = find(bk)
    # 没有关于bk的记录,直接添加内容
    T = True
    a = 'backend %s' % bk
    backup()
    if not r_list:
    with open('HA.txt', 'a') as f1:
    f1.write('\n%s\n'% a)
    f1.write(" %s\n" % sr)
    # 有关于bk的记录
    else:
    # 没有sever记录
    backup()
    if sr not in r_list:
    with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
    for line in f1:
    if line.strip() == 'backend %s' % (bk):
    T = False
    f2.write(line)
    continue
    if T == False and line.strip().startswith('backend'):
    T = True
    if T == True:
    f2.write(line)
    else:
    if sr not in r_list:
    r_list.append(sr)
    t = '\n '.join(r_list)
    f2.write(' '* 8 + t + '\n\n')
    # 有sever记录,直接复制
    else:
    print('记录已存在,请重新选择')
    caidan()
  8.  
  9. def update_bk ():
    '''
    需要修改或删除backend
    :return:
    '''
    backup()
    T = True
    bk = check_bk()
    ls =[]
    with open('HA.txt', 'r') as f1:
    for line in f1:
    if line.strip().startswith('backend') == True:
    to = line.split(' ')
    ls.append(to[].strip())
    c = input('''
    请选择需要选项:
    1 修改backend
    2 删除backend
    ''')
    while T:
    #修改backend信息
    if c == '1':
    bk1 = input('请输入修改后的backend信息')
    t3 = input('信息确认请按 y, 重新输入请按其他键')
    if bk1 in ls:
    print('信息已存在,请重新选择')
    break
    else:
    if t3.lower() == 'y':
    with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
    for line in f1:
    #需要修改的backend
    if line.strip() == 'backend %s' % (bk.strip()):
    f2.write('backend %s \n' % (bk1))
    else:
    f2.write(line)
    print('修改成功')
    break
    else:
    continue
    #删除backend信息
    elif c == '2':
    t = find(bk.strip())
    while T:
    with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
    for line in f1:
    if line.strip() == 'backend %s' % (bk.strip()):
    continue
    if line.strip() in t:
    continue
    else:
    f2.write(line)
    print('删除成功')
    break
    break
  10.  
  11. def check_sr():
    T1 = True
    while T1:
    cont = 0
    a1 = input('sever:')
    ti = a1.split('.')
    for ii in ti:
    if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
    cont = cont + 1
    continue
    else:
    print('输入有误,请输入由四组0-255组成的IP地址')
    break
    if cont == :
    break
    return a1
  12.  
  13. def update_sr():
    '''
    修改或删除sever
    :return:
    '''
    t = check_bk()
    backup()
    T = True
    T1 = True
    ls = find(t.strip())
    cc = 0
    with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:
    for line in f1:
    if line.strip() == 'backend %s' %(t.strip()):
    T = False
    f2.write(line)
    continue
    if T == False and line.strip().startswith('backend'):
    T = True
    if T == True:
    if line.strip() != cc and line.strip() not in ls:
    f2.write(line)
  14.  
  15. else:
    for i, v in enumerate(ls, ):
    print ('%s: %s' % (i, v))
    while T1:
    v1 = input('请输入要修改或删除的编号')
    if v1.isdecimal() and int(v1) in range(len(ls)+):
    break
    else:
    print('输入有误,请重新输入')
    continue
    x = input('请选择 1 = 修改 其他键 = 删除')
    if x == '1':
    while T1:
    a1 = check_sr()
    a2 = input('weight:')
    a3 = input('maxconn:')
    if a2.isdecimal() and a3.isdecimal():
    t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
    print('backend: %s \n%s' % (t, t1))
    else:
    print('所输入weight 和 maxconn 必须为数字,请重新输入')
    continue
    li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
    # server
    if li_sr in ls:
    tt = input('server已存在,请按 y = 重新输入,或按 other key = 返回')
    if tt == 'y':
    continue
    else:
    break
    else:
    a4 = input('请确认信息正确,y = 确认, other key = 重新输入')
  16.  
  17. if a4 == 'y':
    f2.write(' '*8 + li_sr +'\n')
    else:
    continue
    print('修改成功')
    break
    cc = ls[int(v1)-]
    del ls[int(v1)-]
    for tt in ls:
    f2.write(' '*8 + tt +'\n')
    T = True
    else:
    print('删除成功')
    cc = ls[int(v1)-]
    del ls[int(v1)-]
    for tt in ls:
    f2.write(' '*8 + tt +'\n')
    T = True
  18.  
  19. def caidan ():
    T = True
    while T:
    ls = input('''请输入所需编号:
    1 查看backend下sever信息
    2 添加记录
    3 修改或删除backend
    4 修改或删除sever
    5 退出
    ''')
    if ls == '1':
    i = check_bk()
    l1 = find(i.strip())
    for a in l1:
    print (a)
    elif ls == '2':
    while T:
    t = input('请输入backend信息')
    print('请输入sever信息')
    cont = 0
    while T:
    a1 = input('sever:')
    ti = a1.split('.')
    for ii in ti:
    if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
    cont = cont + 1
    continue
    else:
    print('输入有误,请输入由四组0-255组成的IP地址')
    break
    if cont == :
    break
    while T:
    a2 = input('weight:')
    a3 = input('maxconn:')
    if a2.isdecimal() and a3.isdecimal():
    t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
    print('backend: %s \n%s' % (t, t1))
    break
    else:
    print('所输入weight 和 maxconn 必须为数字,请重新输入')
    continue
    t3 = input('信息确认请按 y, 重新输入请按其他键')
    if t3.lower() == 'y':
    add (t, t1)
    print('添加成功')
    break
    else:
    continue
    elif ls == '3':
    update_bk()
    elif ls == '4':
    update_sr()
    elif ls == '5':
    exit()
    else:
    print('输入有误,请重新输入')
  20.  
  21. caidan()

HAproxy配置文件操作的更多相关文章

  1. python之haproxy配置文件操作(第三天)

    作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...

  2. python3 haproxy配置文件操作练习

    哈哈 老规矩 先来一个NLP第六条:咳咳! 六,重复旧的做法,只会得到旧的结果   做法有不同,结果才会有不同. 如果,你的事没有结果,改变你的做法.任何新的做法,都比旧的多一份成功的机会. 想明天比 ...

  3. python作业:HAproxy配置文件操作(第三周)

    一.作业需求: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend ...

  4. 第三周作业HAproxy文件操作

    #coding:utf-8 #Author:Mr Zhi """ HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添 ...

  5. Python3.5 day3作业二:修改haproxy配置文件。

    需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...

  6. haproxy配置文件简单管理

    版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...

  7. s12-day03-work01 python修改haproxy配置文件(初级版本)

    #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo.song ...

  8. haproxy 配置文件详解 之 综述

    HAProxy 配置文件根据功能和用途,主要有5 个部分组成,但有些部分并不是必须的,可以根据需要选择相应的部分进行配置. 1.global 部分 用来设定全局配置参数,属于进程级的配置,通常和操作系 ...

  9. 配置文件操作(ini、cfg、xml、config等格式)

    配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...

随机推荐

  1. webstorm--破解

    2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择"license server" 输入:http://114.215.133.70:41017 2016 ...

  2. busybox的编译、使用及安装

    转载于:http://blog.sina.com.cn/wyw1976 busybox是什么? (1)busybox是Linux上的一个应用程序(application),即只有一个ELF文件头. ( ...

  3. 《编写可维护的JavaScript》——JavaScript编码规范(六)

    变量.函数和运算符 在讨论过基本的JavaScript书写格式化之后,接下来关注如何使用函数.变量和运算符来减少复杂度和增强可读性就显得十分重要了. 变量声明 变量声明是通过var语句来完成的.var ...

  4. org.hibernate.AssertionFailure:collection[......] was not processed by flush()

    八月 12, 2016 11:00:49 上午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() f ...

  5. 封装好的AFN网络请求框架和MBProgress

    demo:https://github.com/IMCCP/CCPAFNNetworking(收藏下来)

  6. 2017年1月2日 星期一 --出埃及记 Exodus 21:28

    2017年1月2日 星期一 --出埃及记 Exodus 21:28 "If a bull gores a man or a woman to death, the bull must be ...

  7. 统计图表类型选择应用总结&表数据挖掘方法及应用

    数据挖掘方法及应用: 图表注意事项 • 信息完整:图表标题.单位.图例.脚注.来源等 • 避免无意义的图表 • 一表反映一个观点 • 只选对的不选复杂的图表 • 标题一句话阐述清楚反映观点 确定对比关 ...

  8. /usr/include/features.h:367:25:fatal errorXXXXXX类似这种问题

    解决方案: sudo apt-get install g++=multilib //至于为什么还没搞清楚,搞清楚在写上来吧!

  9. boost学习笔记(七)---date_time库

    date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...

  10. ReportDesigner中实现保存,保存所有,注册ICommandHandler方式实现

    示例: https://www.devexpress.com/Support/Center/Example/Details/E4354