我使用以下方法通过代理维护目标站点的Keep-Alive连接。
import http.client
proxies = {"https": "https://user:pass@host:port"}
url = urlparse(proxies["https"])
auth = '%s:%s' % (url.username, url.password)
headers['Proxy-Authorization'] = 'Basic '+base64.b64encode(auth.encode('utf-8')).decode()
conn = http.client.HTTPSConnection(proxy.hostname, proxy.port)
conn.set_tunnel("target_site.com",headers=p_headers)
conn.request("POST", "/tapi/", payload, headers)
但是,HTTPSConnection不是线程安全的。我试图通过urllib3进行连接,以便我能够使用多处理。我的代码不起作用。这样做的可能性是什么?
import urllib3
default_headers = urllib3.make_headers(proxy_basic_auth="user:pass")
conn=urllib3.HTTPSConnectionPool('https://target_site.com',maxsize=2,
_proxy="https://host:port/", _proxy_headers=default_headers)
conn.request("POST", "/tapi/",fields=payload, headers=headers)