From fa9a3bbfb5667968531613b24529a364f4d16b29 Mon Sep 17 00:00:00 2001 From: Sergey Shepelev Date: Tue, 14 Jan 2020 00:11:51 +0300 Subject: [PATCH] proxy: username/password as str compatible with pysocks; Thanks to Lorenzo Mentaschi https://github.com/httplib2/httplib2/issues/154 --- python2/httplib2/socks.py | 10 +++++++++- python3/httplib2/__init__.py | 8 ++++---- python3/httplib2/socks.py | 10 +++++++++- tests/test_proxy.py | 4 ++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/python2/httplib2/socks.py b/python2/httplib2/socks.py index 5cef7760..71eb4ebf 100644 --- a/python2/httplib2/socks.py +++ b/python2/httplib2/socks.py @@ -238,7 +238,15 @@ def setproxy( headers - Additional or modified headers for the proxy connect request. """ - self.__proxy = (proxytype, addr, port, rdns, username, password, headers) + self.__proxy = ( + proxytype, + addr, + port, + rdns, + username.encode() if username else None, + password.encode() if password else None, + headers, + ) def __negotiatesocks5(self, destaddr, destport): """__negotiatesocks5(self,destaddr,destport) diff --git a/python3/httplib2/__init__.py b/python3/httplib2/__init__.py index 2f0b0e3a..18021de7 100644 --- a/python3/httplib2/__init__.py +++ b/python3/httplib2/__init__.py @@ -1008,10 +1008,10 @@ def __init__( proxy_headers: Additional or modified headers for the proxy connect request. """ - if isinstance(proxy_user, str): - proxy_user = proxy_user.encode() - if isinstance(proxy_pass, str): - proxy_pass = proxy_pass.encode() + if isinstance(proxy_user, bytes): + proxy_user = proxy_user.decode() + if isinstance(proxy_pass, bytes): + proxy_pass = proxy_pass.decode() self.proxy_type, self.proxy_host, self.proxy_port, self.proxy_rdns, self.proxy_user, self.proxy_pass, self.proxy_headers = ( proxy_type, proxy_host, diff --git a/python3/httplib2/socks.py b/python3/httplib2/socks.py index 2926b4e5..cc68e634 100644 --- a/python3/httplib2/socks.py +++ b/python3/httplib2/socks.py @@ -238,7 +238,15 @@ def setproxy( headers - Additional or modified headers for the proxy connect request. """ - self.__proxy = (proxytype, addr, port, rdns, username, password, headers) + self.__proxy = ( + proxytype, + addr, + port, + rdns, + username.encode() if username else None, + password.encode() if password else None, + headers, + ) def __negotiatesocks5(self, destaddr, destport): """__negotiatesocks5(self,destaddr,destport) diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 375367f4..4ec8aea9 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -32,8 +32,8 @@ def test_from_url_ident(): pi = httplib2.proxy_info_from_url("http://zoidberg:fish@someproxy:99") assert pi.proxy_host == "someproxy" assert pi.proxy_port == 99 - assert pi.proxy_user == b"zoidberg" - assert pi.proxy_pass == b"fish" + assert pi.proxy_user == "zoidberg" + assert pi.proxy_pass == "fish" def test_from_env():