Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy with authentication #154

Closed
menta78 opened this issue Jan 7, 2020 · 10 comments
Closed

proxy with authentication #154

menta78 opened this issue Jan 7, 2020 · 10 comments

Comments

@menta78
Copy link

menta78 commented Jan 7, 2020

At office we have a proxy server with authentication. The environment variables HTTP_PROXY and HTTPS_PROXY have value http://username:password@serveraddress:port .

Using version 0.15.0 of httplib2 (installed from conda-forge) the following code:

http = httplib2.Http()
http.request('http://www.google.com')

results in an AttributeError: 'bytes' object has no attribute 'encode'.
That's because httplib2 encodes the username and password after loading them from the environment variable, and passes them encoded to sock (line 1170 of file __init__.py that I'm using, at line 1174 of the latest commit a8716b1). And then socks tries to re-encode them.

This code works for me:
proxy_info = httplib2.proxy_info_from_environment()
proxy_info.proxy_user = proxy_info.proxy_user.decode()
proxy_info.proxy_pass = proxy_info.proxy_pass.decode()
http = httplib2.Http(proxy_info=proxy_info)
http.request('http://www.google.com')

So, I would suggest either to not encoding these variables in proxy_info_from_environment(), or to decode them before passing them to socks

@temoto
Copy link
Member

temoto commented Jan 8, 2020

@menta78 can't reproduce here. Please, show python -V and traceback.

@menta78
Copy link
Author

menta78 commented Jan 13, 2020

$ python -V
Python 3.7.6

file bug.py:
"""
import httplib2
http = httplib2.Http()
http.request('http://www.google.com')
"""

traceback:
File "bug.py", line 4, in
http.request('http://www.google.com')
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/httplib2/init.py", line 1976, in request
cachekey,
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/httplib2/init.py", line 1640, in _request
conn, request_uri, method, body, headers
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/httplib2/init.py", line 1546, in _conn_request
conn.connect()
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/httplib2/init.py", line 1176, in connect
proxy_pass,
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/socks.py", line 324, in setproxy
return self.set_proxy(*args, **kwargs)
File "/STORAGE/usr/miniconda3/lib/python3.7/site-packages/socks.py", line 318, in set_proxy
username.encode() if username else None,
AttributeError: 'bytes' object has no attribute 'encode'

@menta78
Copy link
Author

menta78 commented Jan 13, 2020

If the proxy server does not require authentication the code works fine

@temoto
Copy link
Member

temoto commented Jan 13, 2020

Thanks, it didn't reproduce because failing line is not in httplib2 source. But the problem is very clear now.

@temoto
Copy link
Member

temoto commented Jan 13, 2020

Could you also share where this site-packages/socks.py comes from?

Something like pip freeze |fgrep -i socks

@temoto
Copy link
Member

temoto commented Jan 13, 2020

Nevermind, I found it's pysocks.

@temoto
Copy link
Member

temoto commented Jan 13, 2020

@menta78 fix is merged in master fa9a3bb

Release machinery is awoken too.

@temoto
Copy link
Member

temoto commented Jan 13, 2020

Please check if it works and I wasn't too fast to merge it?

@menta78
Copy link
Author

menta78 commented Jan 15, 2020

now it works for me.

Many thanks!

@temoto
Copy link
Member

temoto commented Jan 16, 2020

Released as v0.16.0
Thank you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants