gpt4 book ai didi

Python SOCKS5 代理客户端 HTTPS

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:17 27 4
gpt4 key购买 nike

我尝试通过 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\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 发出 HTTP 请求(例如 GET example.com HTTP/1.1 而不是 GET/HTTP/1.1 code>),这种行为可能会导致问题。

关于Python SOCKS5 代理客户端 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com