在代理python上重写http请求
我需要一些关于性能调优的建议这个脚本读取代理在临时ram磁盘上转储的URL头,它读取文件并将其附加到列表中,在一些检查之后,它读取列表,如果该行包含“用户代理”重拨并使用标准输出刷新在代理python上重写http请求,python,linux,proxy,Python,Linux,Proxy,我需要一些关于性能调优的建议这个脚本读取代理在临时ram磁盘上转储的URL头,它读取文件并将其附加到列表中,在一些检查之后,它读取列表,如果该行包含“用户代理”重拨并使用标准输出刷新 proc = open(sys.argv[1],'r') slog.write("writing standard input \n") for line in proc.readlines(): header.append(line) . . . . . . . .
proc = open(sys.argv[1],'r')
slog.write("writing standard input \n")
for line in proc.readlines():
header.append(line)
. . . . . . . .
if check_header == None: #check_header is returned by one of the functions to whether rewrite the header
for h in header:
if "User-Agent" in h and "custom-header:" not in h:
h = h.rstrip("\r\n") + " custom-header:" + customer + "\r\n"
sys.stdout.write(h)
sys.stdout.flush()
#sys.exit(1)
else:
sys.stdout.write(new_get)
我担心的是,对于大量的请求,它的速度会很慢,因为它会附加到列表中,读取并刷新它,如果不知道如何对其进行性能调整,除非代码示例中的缩进错误,否则您将尝试添加自定义头,添加次数与列表中的元素数量相同。试一试
proc = open(sys.argv[1],'r')
slog.write("writing standard input \n")
for h in proc.readlines():
. . . . . . . .
if check_header == None: #check_header is returned by one of the functions to whether rewrite the header
if "User-Agent" in h and "custom-header:" not in h:
h = h.rstrip("\r\n") + " custom-header:" + customer + "\r\n"
sys.stdout.write(h)
sys.stdout.flush()
else:
sys.stdout.write(new_get)
没有理由在此处使用
.readlines()
。您可以迭代文件
对象。还有,为什么每次添加新元素时都要迭代标题?这是因为我在所有请求上向用户代理插入自定义标题Hank lliyan,这确实有帮助,因为我不再使用列表,但我认为这不会提高任何性能。。。