0

我需要一些关于性能调整此脚本的建议,该脚本读取代理在临时 ram 磁盘上转储的 URL 标头,它读取文件并将其附加到列表中,经过一些检查后它会读取列表,并且如果该行包含“用户代理” " 使用标准输出重新编辑并刷新...

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)

我担心的是,对于大量请求,它会很慢,因为它附加到列表,读取并刷新它,任何想法我可以如何调整它

4

1 回答 1

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)
于 2012-10-11T18:52:50.343 回答