request.get() 中未遵守超时

timeout not respected in request.get()

我不明白为什么使用此代码 requests.get() returns 会立即出现错误并且不遵守 10 秒超时。 如果我删除 header 参数,那么只有 requests.get(url, proxies={"http": proxy, "https": proxy}, verify=False,timeout=10),超时会被遵守并且请求成功返回,如果超时则返回失败。 我还需要插入 headers 和超时来测试代理,该怎么做?

from fake_useragent import  UserAgent
import requests
ua = UserAgent()

#get a list of proxy 

for i in range(1, len(proxies)):
    # Get a proxy from the pool
    proxy = next(proxy_pool)
    print("Request #%d" % i)
    try:
        response = requests.get(url, headers=ua.random(), proxies={"http": proxy, "https": proxy}, verify=False,timeout=10)
        break
    except:
        print("Skipping. Connnection error")

试试这个

ua = UserAgent()
header = {'User-Agent':str(ua.random)}
response = requests.get(url, headers=header, verify=False,timeout=10)