【问题标题】:Python SOCKS5 proxy client HTTPSPython SOCKS5 代理客户端 HTTPS
【发布时间】:2017-12-06 15:18:06
【问题描述】:

我试图通过 SOCKS5 代理服务器通过 HTTPS 发出请求,但它失败或返回空字符串。我正在使用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 请求?

谢谢

【问题讨论】:

  • 你能提到你导入的库吗?
  • @AakashVerma 感谢提供的解决方案工作正常,请发表您的评论作为答案
  • 好的。很高兴帮助:)

标签: python python-2.7 https proxy socks


【解决方案1】:

截至 2016 年 4 月 29 日发布的请求版本 2.10.0,请求 支持袜子。

需要 PySocks,可以通过 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 请求,这种行为可能会导致问题。

【讨论】:

    猜你喜欢
    • 2011-12-24
    • 2017-02-21
    • 1970-01-01
    • 2013-10-08
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    相关资源
    最近更新 更多