Python SOCKS5 代理客户端 HTTPS

Python SOCKS5 proxy client HTTPS

我正在尝试通过 SOCKS5 代理服务器通过 HTTPS 发出请求,但失败或 returns 为空字符串。我正在使用 PySocks 库。

这是我的例子

    WEB_SITE_PROXY_CHECK_URL = "whatismyipaddress.com/"
    REQUEST_SCHEMA = "https://"

    host_url = REQUEST_SCHEMA + WEB_SITE_PROXY_CHECK_URL
    socket.connect((host_url, 443))
    request = "GET / HTTP/1.1\nHost: " + host_url + "\nUser-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11\n\n"
    socket.send(request)
    response = socket.recv(4096)
    print response

但是它不起作用,它打印一个空字符串响应。

在Python中有什么方法可以通过 socks5 代理发出 HTTPS 请求吗?

谢谢

As of requests version 2.10.0, released on 2016-04-29, requests supports SOCKS.

It requires PySocks, which can be installed with pip install pysocks.

 import requests

 host_url = 'https://example.com'
 #Fill in your own proxies' details
 proxies={http:'socks5://user:pass@host:port',
          https:'socks5://user:pass@host:port'}
 #define headers if you will
 headers={}

 response = requests.get(host_url, headers=headers, proxies=proxies)

请注意,当使用 SOCKS 代理时,请求 socks 将使用完整的 URL(例如,GET example.com HTTP/1.1 而不是 GET / HTTP/1.1)发出 HTTP 请求,这种行为可能会导致问题。