PySocks 代理身份验证失败 [英] PySocks proxy auhtentication failed
问题描述
我仍在尝试创建 imap\pop3 代理客户端.第一次尝试是在 PHP 上这里.现在我用 python 尝试同样的方法.
I'm still try create imap\pop3 proxied client. First try is on PHP is here. Now I try the same with python.
我找到了一个 答案,很适合,所以我的代码基于 stackowerflow 答案,它无需身份验证即可用于代理:
I found an answer, which good fits, so my code based is on stackowerflow answer and it work for proxy without authentication:
class SocksIMAP4SSL(IMAP4_SSL):
def open(self, host, port=IMAP4_SSL_PORT):
self.host = host
self.port = port
self.sock = create_connection(dest_pair=(host, port), proxy_type=PROXY_TYPE_HTTP, proxy_addr=PROXY_IP,
proxy_port=PROXY_PORT, proxy_rdns=True, proxy_username=PROXY_AUTH_LOGIN,
proxy_password=PROXY_AUTH_PASS)
# self.sock = socket.create_connection((host, port))
self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
self.file = self.sslobj.makefile('rb')
但是如果我使用代理进行身份验证,它总是会因以下原因而失败:
But if I use proxy with authentication it always fail by reason:
socks.HTTPError: 407: Proxy Authentication Required
代理身份验证类型:基本.登录名和密码都正确.我做错了什么?
Proxy auth type: basic. Both login and password is correct. What I am doing wrong?
推荐答案
我使用了 https://github.com/Anorov/PySocks,但它不支持 HTTP 代理的基本身份验证.几行代码解决了这个问题:
I used https://github.com/Anorov/PySocks, but It does not support basic authentication for HTTP proxy. A few lines solves this issue:
http_headers = [
b"Connect %s:%s HTTP/1.1" % (addr.encode('idna'), str(dest_port).encode()),
b"Host: %s" % dest_addr.encode('idna')
]
if username and password:
http_headers.append(b"Proxy-Authorization: basic %s" % str(b64encode("%s:%s" % (username, password))))
http_headers.append(b"\r\n")
self.sendall(b"\r\n".join(http_headers))
这篇关于PySocks 代理身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!