python编写查询_python开发【七】---查询

haproxy.conf

global

log 127.0.0.1 local2

daemon

maxconn 256

log 127.0.0.1 local2 info

defaults

log global

mode http

timeout connect 5000ms

timeout client 50000ms

timeout server 50000ms

option dontlognull

listen stats :8888

stats enable

stats uri /admin

stats auth admin:1234

frontend oldboy.org

bind 0.0.0.0:80

option httplog

option httpclose

option forwardfor

log global

acl www hdr_reg(host) -i www.oldboy.org

use_backend www.oldboy.org if www

backend www.oldboy1.org

server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30

server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000

server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000

server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000

backend www.oldboy2.org

server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000

backend www.oldboy20.org

server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333

import os

if __name__ == "__main__": #变量

msg= """

1:查询

2:添加

3:修改

4:删除

5:退出

"""

msf_dic={"1":fetch,"2":add(),"3":change,"4":delete}

while True:

print(msg)

choice=input("请输入你的选项:").strip()

if not choice:continue

if choice == "5":break

data=input("请输入你的数据:").strip()

##############查询####################

def fetch(data):

# print("这是查询功能")

backend_data = 'backend %s' % data

with open("haproxy.conf","r") as read_f:

tag = False # tag标识一种状态

ret=[]

for read_line in read_f:

if read_line.strip() == backend_data:

tag=True

continue

if tag and read_line.startswith("backend"):

break

if tag:

print(read_line,end="")

ret.append(read_line)

return ret

##############修改#################

def change(data):

print("这是修改功能")

print("用户输入的数据是",data)

backend=data[0]["backend"] #文件当中的一条记录

backend_data="backend %s" %backend

old_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[0]["record"]["server"],

data[0]["record"]["server"],

data[0]["record"]["weight"],

data[0]["record"]["maxconn"])

new_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[1]["record"]["server"],

data[1]["record"]["server"],

data[1]["record"]["weight"],

data[1]["record"]["maxconn"])

print("用户想要修改的记录是",old_server_record)

res=fetch(backend)

if not res or old_server_record not in res:

return "修改记录不存在"

else:

index=res.index(old_server_record)

res[index]=new_server_record

res.insert(0,"%s\n" %backend)

with open("haproxy.comf","r") as read_f,\

open("haproxy.comf_new","w") as write_f:

tag=False

has_write=False

for read_line in read_f:

if read_line.strip() == backend_data:

tag=True

continue

if tag and read_line.startswith("backend")

tag=False

if not tag:

write_f.write(read_line)

else:

if not has_write:

for record in res:

write_f.write(record)

has_write=True

os.rename("haproxy.conf","haproxy.conf.bak")

os.rename("haproxy.conf_new","haproxy.conf")

os.remove("haproxy.conf.bak")

###########最终版本##########解耦和

import os

def file_handler(backend_data,res=None,type="fetch"):

if type == "fetch":

with open("haproxy.conf", "r") as read_f:

tag = False

ret = []

for read_line in read_f:

if read_line.strip() == backend_data:

tag = True

continue

if tag and read_line.startswith("backend"):

break

if tag:

print(read_line, end="")

ret.append(read_line)

return ret

elif type == "change":

with open("haproxy.comf","r") as read_f,\

open("haproxy.comf_new","w") as write_f:

tag=False

has_write=False

for read_line in read_f:

if read_line.strip() == backend_data:

tag=True

continue

if tag and read_line.startswith("backend"):

tag=False

if not tag:

write_f.write(read_line)

else:

if not has_write:

for record in res:

write_f.write(record)

has_write=True

os.rename("haproxy.conf", "haproxy.conf.bak")

os.rename("haproxy.conf_new", "haproxy.conf")

os.remove("haproxy.conf.bak")

def fetch(data):

# print("这是查询功能")

backend_data = 'backend %s' % data

return file_handler(backend_data)

def add():

pass

def change(data):

print("这是修改功能")

print("用户输入的数据是",data)

backend=data[0]["backend"] #文件当中的一条记录

backend_data="backend %s" %backend

old_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[0]["record"]["server"],

data[0]["record"]["server"],

data[0]["record"]["weight"],

data[0]["record"]["maxconn"])

new_server_record="%sserver %s %s weight %s maxconn %s\n" %(" "*8,data[1]["record"]["server"],

data[1]["record"]["server"],

data[1]["record"]["weight"],

data[1]["record"]["maxconn"])

print("用户想要修改的记录是",old_server_record)

res=fetch(backend)

if not res or old_server_record not in res:

return "修改记录不存在"

else:

index=res.index(old_server_record)

res[index]=new_server_record

res.insert(0,"%s\n" %backend)

file_handler(backend_data,res=res,type="change")

def delete():

pass

if __name__ == "__main__": #变量

msg= """

1:查询

2:添加

3:修改

4:删除

5:退出

"""

msf_dic={"1":fetch,"2":add(),"3":change,"4":delete}

while True:

print(msg)

choice=input("请输入你的选项:").strip()

if not choice:continue

if choice == "5":break

data=input("请输入你的数据:").strip()

if choice != "1":

data=eval(data) #eval提取字符串中的数据结构,也可以执行字符串种的运算表达式

res=msf_dic[choice](data)

print(res)

----------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值