在代理python上重写http请求

时间:2012-10-11 18:32:35

标签: python linux proxy

我需要一些关于性能调优的建议,该脚本读取代理在临时ram磁盘上转储的URL头,它读取文件并将其附加到列表中,经过一些检查后它读取List并且该行包含“ User-Agent“redit并使用标准输出刷新......

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)

我担心的是,对于大量的请求,它会变得很慢,因为它会附加到列表中,阅读它并将其清除掉,任何想法我如何能够对其进行性能调整

1 个答案:

答案 0 :(得分:0)

除非代码示例中的缩进错误,否则您尝试添加自定义标头的次数与列表中的许多元素一样多。尝试

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)