Python SOCKS5代理客户端HTTPS

人气:897 发布:2022-10-16 标签: https python proxy socks python-2.7

问题描述

我试图通过HTTPS上的SOCKS5代理服务器发出请求,但失败或返回空字符串。我正在使用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
Host: " + host_url + "
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11

"
    socket.send(request)
    response = socket.recv(4096)
    print response

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

有没有办法在Python中通过socks5代理发出HTTPS请求?

谢谢

推荐答案

截至2016-04-29发布的请求版本2.10.0,请求 支持袜子。

它需要使用可以与pip安装pysock一起安装的PySock。

 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请求,此行为可能会导致问题。

417