python---haproxy---文件操作
haproxy 文件操作,操作属于简单操作,不复杂
# -*- coding:utf-8 -*-
# LC def search(*args): #查找Haproxy文件中的服务器
list1 = []
with open("haproxyfile","r") as f:
flag = False
for line in f:
if line.strip() == "backend %s" % args: #查找第一个以backend + 输入域名的起头的backend,将相关信息记录list中
flag = True
list1.append(line.strip())
continue #并继续循环,为了将backend下一行执行,即按着flag = True执行
if line.strip().startswith("backend") or line.strip() == "": #查到第二个backend这将flag置于Fasle,如果是空行,也将flag置于Fasle中,(防止文章末尾有多个空行)
flag = False
if flag:
list1.append(line.strip()) #将正确backend的后续信息继续记录至list中
return list1 def delete(string):
dict = eval(string)
backend = dict["backend"]
record = dict["record"]
new_file_list = []
if backend in domain_list(): #判读要删除的域名是否在文件内
with open("haproxyfile","r",encoding="utf-8") as f_read:
for line in f_read:
new_file_list.append(line) #读取文件的每行,将每行写入列表
if line.strip() == "backend %s" %backend: #如果有符合的backend,则将最近的写入列表弹出
new_file_list.pop()
if line.strip() == "server %s weight %s maxconn %s" %(record["server"],record["weight"],record["maxconn"]):#如果有符合的server信息,则将最近的写入列表弹出
new_file_list.pop()
with open("haproxyfile1", "a") as f_write: #将删除后的写入文件中
for line in new_file_list:
f_write.write(line)
else:
print("The Domain not in this file!") def add(string): #增加ha文件配置服务器信息
dict = eval(string)
backend = dict["backend"]
record = dict["record"]
with open("haproxyfile","a") as f_write:
f_write.write("backend %s\n"%backend)
f_write.write("\t\tserver %s %s weight %s maxconn %s"%(record["server"][0],record["server"][1],
record["weight"],
record["maxconn"])) def domain_list(): #将文件中所有的domain都摘出来
backend_name = []
with open("haproxyfile","r") as f_read:
for line in f_read:
if line.startswith("backend"):
domain_name = line.split()[1]
backend_name.append(domain_name)
return backend_name #域名信息查找
domain_search_name = input("Please input the domain your search:")
if domain_search_name in domain_list():
domain_info = search(domain_search_name)
print(domain_info) #域名信息删除 string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}"
delete(string) #域名信息增加
string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}"
add(string)
收集backend信息和对应server的信息
def backend_info():
with open("haproxyfile","r") as f_read:
backend_list = []
dict_all = {}
dict_ser = {}
server_list = []
for line in f_read:
if line.strip().startswith("backend"):
backend_name = line.split()[1]
backend_list.append(backend_name)
server_list = []
if line.strip().startswith("server"):
dict_ser['server'] = line.strip().split()[1]
dict_ser['weight'] = line.strip().split()[3]
dict_ser['maxconn'] = line.strip().split()[5]
server_list.append(dict_ser)
dict_all[backend_name] = server_list
return backend_list,dict_all #返回backend的所有域名信息和域名包含的服务器信息
python---haproxy---文件操作的更多相关文章
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- Python 常见文件操作的函数示例(转)
转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 孤荷凌寒自学python第三十三天python的文件操作初识
孤荷凌寒自学python第三十三天python的文件操作初识 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天开始自学python的普通 文件操作部分的内容. 一.python的文件打开 ...
- python中文件操作的六种模式及对文件某一行进行修改的方法
一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
- Python常见文件操作的函数示例
# -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...
- python的文件操作及简单的用例
一.python的文件操作介绍 1.文件操作函数介绍 open() 打开一个文件 语法:open(file, mode='r', buffering=-1, encoding=None, errors ...
- python基本文件操作
python文件操作 python的文件操作相对于java复杂的IO流简单了好多,只要关心文件的读和写就行了 基本的文件操作 要注意的是,当不存在某路径的文件时,w,a模式会自动新建此文件夹,当读模式 ...
- [转]python file文件操作--内置对象open
python file文件操作--内置对象open 说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...
随机推荐
- PO核准通知界面修改
想在notification頁面把供應商的稅捐代碼帶出來,添在如下紅框中 PO_WF_PO_NOTIFICATION head information:get_po_approve_msg line ...
- Java编写的接口测试工具
这几天由于要频繁地使用一些天气数据接口,但是每次都要频繁的打开网页,略显繁琐,故就自己做了两个json数据获取的小工具. 第一个 先来看看第一个吧,思路是使用一个网络流的处理,将返回的json字符串数 ...
- cd 命令的具体使用说明
cd 命令的具体使用说明
- Google的两种广告推广方式
1搜索关键字广告推送:AdWords: 覆盖广泛:在全球最大的搜索和网络平台上进行推广. 定位精准:锁定目标客户群体,让潜在客户轻松找上门. 成本可控:仅当用户点击广告时,您才支付费用. 2.网站内容 ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- ISLR系列:(4.1)模型选择 Subset Selection
Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...
- Android Widget工作原理详解(一) 最全介绍
转载请标明出处:http://blog.csdn.net/sk719887916/article/details/46853033 ; Widget是安卓的一应用程序组件,学名窗口小部件,它是微型应用 ...
- 【云计算 Hadoop】Hadoop 版本 生态圈 MapReduce模型
忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...
- Android官方技术文档翻译——清单合并
本文译自Android官方技术文档<Manifest Merger>,原文地址:http://tools.android.com/tech-docs/new-build-system/us ...
- 【翻译】了解ASP.NET MVC中的Ajax助手
原文:Understanding AJAX Helpers in ASP.NET MVC 作者: Shailendra Chauhan works as Software Analyst at rep ...