This is just about the most simple snippet of how to do proxy authentication with SSL using python. The current httplib only supports ssl through a proxy _without_ authentication. This example does basic proxy auth that a lot of proxy servers can support. This will at least give someone an idea of how to do it and then improve it and incorporate it however they want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import httplib,base64,socket user='proxy_login';passwd='proxy_pass' host='login.yahoo.com';port=443 phost='proxy_host';pport=80 #setup basic authentication user_pass=base64.encodestring(user+':'+passwd) proxy_authorization='Proxy-authorization: Basic '+user_pass+'\r\n' proxy_connect='CONNECT %s:%s HTTP/1.0\r\n'%(host,port) user_agent='User-Agent: python\r\n' proxy_pieces=proxy_connect+proxy_authorization+user_agent+'\r\n' #now connect, very simple recv and error checking proxy=socket.socket(socket.AF_INET,socket.SOCK_STREAM) proxy.connect((phost,pport)) proxy.sendall(proxy_pieces) response=proxy.recv(8192) status=response.split() if status!=str(200): raise 'Error status=',str(status) #trivial setup for ssl socket ssl = socket.ssl(proxy, None, None) sock = httplib.FakeSocket(proxy, ssl) #initalize httplib and replace with your socket h=httplib.HTTPConnection('localhost') h.sock=sock h.request('GET','/') r=h.getresponse() print r.read()
This snippet gives you the basic approach of how to do proxy authentication with ssl. Connect to port 80 of the proxy server, send your authentication string, make a socket-like SSL connection, and then finally use the httplib infrastructure by giving it the "ssl socket".
Ultimately, httplib.HTTPSConnection should contain proxy code like this instead of presenting the socket to HTTPConnection manually.
You can reach me at pyguy2 on yahoo.
Error... When I execute this program, it shows me the following error...
File "", line 1, in connect
gaierror: (7, 'getaddrinfo failed')
How should I fix this ?
Did you change the following to a meaningful hostname?
Having problems creating ssl connection to proxy. This code looks like soemthing I am trying to tackle, so I thought I would look at this code. In summary, we need to CONNECT to an HTTPS url via a proxy. Anyway, I tried this code, taking care to update the phost and pport to match the proxy I need to go through. I also updated the user name and password to my username and password on yahoo. Anyway, in short, the proxy socket connection works fine and the CONNECT request comes through with status 200, so far so good. But when the socket.ssl connection is attempted, I get the following error:
sslerror (8, 'EOF occurred in violation of protocol')
and of course the program aborts. Any idea as to the cause? Thanks.
ssl connection problems. I had this problem using Python 2.4 / windows, through to a Microsoft ISA proxy. The problem turned out to be caused by later versions of SSL. I downloaded M2Crypto (windows installer available) thanks to Ng Pheng Siong, which provides SSL version 3. M2Crypto is compatible with the Microsoft ISA proxy server (and I believe squid). The M2Crypto distribution contains a working proxy client in the contrib directory.
I ran straight into a problem with authentication. Luckily the latest version of pywin (204) contains an SSPI library (windows only) which can authenticate with the proxy server.
Error message. This is a error message after i execute the program, where is problem ?
Traceback (most recent call last):
File "Q:\urllib\test2.py", line 23, in ?
File "C:\Python\lib\socket.py", line 73, in ssl
socket.sslerror: (8, 'EOF occurred in violation of protocol')
Sorry. Very sorry all, my problem is probably same as problem in previous comment.
Now I have problem with M2Crypto. If I want used it, I see folowing error:
I download M2Crypto from M2Crypto webpage, but __m2crypto.py not in build\lib.win32-2.3\M2Crypto directory.
(8, 'EOF occurred in violation of protocol'). Hello all, problem with this error is here:
This is fixed:
Problem is in three end of lines in request. The request must include only two end of lines.
I removed redundant \r\n. I removed that small bug, thanks for noticing it.
The problem. Would you care to post your solution? Or, alternatively, email me? Thanks, Vlad
8, 'EOF occurred in violation of protocol' This may have been obvious to everyone except a noob like myself, but for some reason (at least in python 2.5.1) base64.encodestring puts a '\n' at the end of the encoded string. This seems to cause the above error. After spending 2 days trying to figure out why I couldn't get this recipe to work, I added the following line:
username_password = username_password.replace('\n','')
and everything now works perfectly. I hope this saves someone some time.