# str = r'''
# 《春晓》这首小诗,初读似觉平淡无奇,反复读之,便觉诗中别有天地。它的艺术魅力不在于华丽的辞藻,不在于奇绝的艺术手法,而在于它的韵味。整首诗的风格就像行云流水一样平易自然,然而悠远深厚,独臻妙境。千百年来,人们传诵它,探讨它,仿佛在这短短的四行诗里,蕴涵着开掘不完的艺术宝藏。
# 自然而无韵致,则流于浅薄;若无起伏,便失之平直。《春晓》既有悠美的韵致,行文又起伏跌宕,所以诗味醇永。诗人要表现他喜爱春天的感情,却又不说尽,不说透,“迎风户半开”,让读者去捉摸、去猜想,处处表现得隐秀曲折。
# “情在词外曰隐,状溢目前曰秀
# 春眠不觉晓,处处闻啼鸟。
# 夜来风雨声,花落知多少。
# '''
# with open('info.txt','w',encoding='utf-8') as myfile:
# myfile.write(str)
# case 1. 请求URL--> 下载文件
# import requests
# url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
# headers = {
# 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
# }
# res = requests.get(url,headers)
# data = res.content # 读取 res 中的内容
# with open('demo.mp4','wb') as myfile: # 二进制写入 mp4
# myfile.write(data)
# myfile.flush()
# case 2. 日志计数
# total = 0
# ip = "118.8.8.8"
# with open('log.txt','r',encoding='utf-8') as myfile:
# for line in myfile:
# if line.startswith(ip): # 是否以 118.8.8.8 开头
# total += 1
# print(total)
# case 3. 日志计数加强版
# 118.8.8.8 - this is a test
# user_dict = {}
# with open('log.txt','r',encoding='utf-8') as myfile:
# for line in myfile: # 读取 每行 内容
# ip = line.split('-')[0] # 通过 - 进行分割,取出IP
# if ip in user_dict: # 如果 ip 在字典key里
# user_dict[ip] += 1 # -> ip的值 +1
# else:
# user_dict[ip] = 1 # -> 第1次出现,设置为1
# print(user_dict)
# case 4. 股票数据(跳行读)
'''找出>2的guo'''
# with open('satock.txt','r',encoding='utf-8') as myfile:
# myfile.readline() # 读1行(即跳过这行)
# for line in myfile: # 从第2行开始读
# text = line.split(',')[1] # 取出价格这例
# price = float(text) # ->转换为浮点
# if price >=2: # 如果 价格 >=2,则打印这行
# print(line.strip())
# case 5. 处理ha.conf
'''寻找字符串并替换'''
import shutil
old_site = "www.abc.com"
new_site = "www.xyz.com"
# -> 同时打开读文件 'ha.conf,写文件 'ha_new.conf'
with open('ha.conf','r',encoding='utf-8') as read_file_object,open('ha_new.conf','w',encoding='utf-8') as write_file_object:
for line in read_file_object: # 读取1行文件内容
new_line = line.replace(old_site,new_site) # 如果,是wwww.abc.com,则替换为 wwww.xyz.com。如果不是,保留原行内容
print(new_line.strip())
write_file_object.write(new_line) # 写入新文件
shutil.move('ha_new.conf','ha.conf') # 覆盖 并 重命名 文件名
对应的文件:
---->> log.txt
118.8.8.8 - this is a test
118.8.8.9 - this is a test
118.8.8.10 - this is a test
118.8.8.8 - this is a test
118.8.8.11 - this is a test
118.8.8.12 - this is a test
118.8.8.8 - this is a test
--->> satock.txt
板块名称,涨跌幅,领涨股票
电力行业,1.34,深南电A
供水供气,3.09,大连热电
石油行业,2.07,恒泰艾普
玻璃行业,1.00,菲利华
有色金属,0.93,豫光金铅
--->> ha.conf
global ##全局配置
maxconn 10000
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local2 info
user haproxy ##指定用户
group haproxy ##指定组
chroot /usr/local/haproxy ##服务工作目录
daemon ##开启保护进程
mode http
option httplog
log global
timeout client 1m
timeout server 1m
timeout connect 10s
timeout http-keep-alive 2m
timeout queue 15s
timeout tunnel 4h # for websocket
default-server inter 1000 weight 3
listen app1 ##此部分是前端部分和后端部分的结合
bind :80 ##监听的端口
log global
server web1 www.abc.com:80 check ##后端的真实服务器地址
server web2 www.abc.com:80 check ##后端的真实服务器地址