首页 > 解决方案 > Urllib 提供假代理

问题描述

我用这个脚本测试我的代理,它提供了很多工作,但是当我用另一个代理检查器测试“工作”代理时,只有很少量的工作。

这是检查代理是否有效的部分:

def process(self, task):
    global alive
    global dead
    global tested
    proxy = task
    log_msg = str("Trying HTTP proxy%21s " % proxy)

    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(
                urllib.request.HTTPCookieProcessor(cj),
                urllib.request.HTTPRedirectHandler(),
                urllib.request.ProxyHandler({'http': proxy})
    )

    try:
        t1 = time.time()
        response = opener.open(test_url, timeout=timeout_value).read()
        tested += 1
        t2 = time.time()
    except Exception as e:
        log_msg += "%s " % fail_msg
        print(Fore.LIGHTRED_EX + log_msg)
        dead += 1
        tested += 1
        return None

    log_msg += ok_msg + "Response time: %d" % (int((t2-t1)*1000))
    print(Fore.LIGHTGREEN_EX + log_msg)
    text_file = open(out_filename, "a")
    text_file.write(proxy + "\r\n")
    text_file.close()
    alive += 1

标签: pythonpython-3.xproxyurllibhttp-proxy

解决方案


推荐阅读