程序2:修改haproxy配置文件
需求:
1 1、查 2 输入:www.oldboy.org 3 获取当前backend下的所有记录 4 5 2、新建 6 输入: 7 arg = { 8 'bakend': 'www.oldboy.org', 9 'record':{10 'server': '100.1.7.9',11 'weight': 20,12 'maxconn': 3013 }14 }15 16 3、删除17 输入:18 arg = {19 'bakend': 'www.oldboy.org',20 'record':{21 'server': '100.1.7.9',22 'weight': 20,23 'maxconn': 3024 }25 }
1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms10 timeout client 50000ms11 timeout server 50000ms12 option dontlognull13 14 listen stats :888815 stats enable16 stats uri /admin17 stats auth admin:123418 19 frontend oldboy.org20 bind 0.0.0.0:8021 option httplog22 option httpclose23 option forwardfor24 log global25 acl www hdr_reg(host) -i www.oldboy.org26 use_backend www.oldboy.org if www27 28 backend www.oldboy.org29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
删除的方法是使用一个很蠢的方式的,必须另外加个临时文件来修改,这样的话,如果是很大的文件就没办法了。
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author linux ku 4 #{'backend': 'www.oldboy.org','record':{ 'server': '100.1.7.9','weight': 20,'maxconn': 30}} 5 #{'backend': 'www.cool2.org','record':{ 'server': '100.2.8.9','weight': 30,'maxconn': 70}} 6 def recd_add(): 7 '''用来增加数据,暂时没有加入判断输入数据类型的内容''' 8 add_inf_tmp = input('请输入要增加的内容') 9 add_inf = eval(add_inf_tmp)10 with open('G:\学习\Python\learnning\homework3\Record.txt', 'r+') as file: #用可以读写的形式打开11 for line in file: #用来判断是否数据已存在12 if 'backend' in line:13 line_ful = "backend {website}\n".format(website=add_inf['backend'])14 if line == line_ful:15 return print('The record already exits.')16 file.write('\nbackend {backend}\n '.format(backend = add_inf['backend']))17 file.write('\tserver {red1} weight {num1} maxconn {num2}\n'.format(red1=add_inf['record']['server'], #记得读取是要用这个形式的,不能用数字哦,数字那个是列表18 num1=add_inf['record']['weight'],19 num2=add_inf['record']['maxconn']))20 return print('Adding record successfully. ')21 22 23 def recd_read():24 '''用来读取数据'''25 read_inf = (input('请输入要读取的地址'))26 with open('G:\学习\Python\learnning\homework3\Record.txt', 'r') as file: #用可以读写的形式打开27 for line in file: #用来判断是否数据已存在28 if 'backend' in line:29 line_ful ="backend {website}\n".format(website=read_inf)30 if line == line_ful:31 rd = file.readline()32 return print(rd) #这里要用print,不然输出不了33 return 'The record do not exit. '34 35 36 def recd_delete_stupid():37 '''用来删除数据,先用个笨办法吧'''38 del_inf = eval(input('请输入要删除的地址的内容'))39 index_old_file = 0 #两个指针来记录行数变化,来判断输入数据是否正确40 index_new_file = 041 with open('G:\学习\Python\learnning\homework3\Record.txt', 'r+') as file: #用可以读写的形式打开42 file_tmp = open('G:\学习\Python\learnning\homework3\Record_tmp.txt', 'w')43 line_ful = "backend {website}\n".format(website=del_inf['backend'])44 for line in file: #用来新的文件来记录不包含待删除行的内容45 if 'backend' not in line:46 file_tmp.write(line)47 index_new_file += 148 index_old_file += 149 elif line != line_ful:50 file_tmp.write(line)51 index_new_file += 152 index_old_file += 153 else:54 print('Delete {line}\n \t{content}'.format(line = line, content = file.readline()))55 index_old_file += 156 file_tmp.close() #转换模式,不然无法读取57 file_tmp = open('G:\学习\Python\learnning\homework3\Record_tmp.txt', 'r')58 if index_old_file == index_new_file:59 file_tmp.close() #记得关闭60 return print('The input content is wrong!')61 else:62 with open('G:\学习\Python\learnning\homework3\Record.txt', 'w') as file: # 用写模式打开,准备覆盖63 for line in file_tmp:64 file.write(line)65 file_tmp.close() #记得关闭66 return print('The record has been delete. ')67 68 69 choice_recd = ['add', 'delete', 'read','quit'] #用来判断输入的内容对不对70 while True:71 choice = input("Please input 'add', 'delete', 'read','quit'")72 if choice in choice_recd: #判断输入的内容对不对73 if choice == 'add':74 recd_add()75 elif choice == 'read':76 recd_read()77 elif choice == 'delete':78 recd_delete_stupid()79 elif choice == 'quit':80 break
http://bbs.csdn.net/topics/340255616
这个链接里面有点讨论的内容。
也就是说,文件的修改和读写还可以学习,seek和writelines