首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在代理python上重写http请求

在代理python上重写http请求
EN

Stack Overflow用户
提问于 2012-10-12 02:32:35
回答 1查看 425关注 0票数 0

我需要一些关于性能调整的建议这个脚本读取由临时内存磁盘上的代理转储的URL头,它读取文件并将其附加到列表中,在一些检查后,它读取列表,如果行包含"User-Agent“编辑,并使用标准输出清除...

代码语言:javascript
复制
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)

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

EN

回答 1

Stack Overflow用户

发布于 2012-10-12 02:52:50

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12846253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档