Welcome, guest | Sign In | My Account | Store | Cart

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.

Python, 31 lines
 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()[1]
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.

10 comments

Shanker K 19 years, 5 months ago  # | flag

Error... When I execute this program, it shows me the following error...

proxy.connect((phost,pport))

File "", line 1, in connect

gaierror: (7, 'getaddrinfo failed')

How should I fix this ?

Kartic K 19 years, 5 months ago  # | flag

Did you change the following to a meaningful hostname?

phost='proxy_host'
Andrew Bushnell 18 years, 11 months ago  # | flag

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.

Kevin Gill 18 years, 9 months ago  # | flag

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.

Tomas Brabenec 18 years, 6 months ago  # | flag

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 ?

ssl = socket.ssl(proxy, None, None)

File "C:\Python\lib\socket.py", line 73, in ssl

return _realssl(sock, keyfile, certfile)

socket.sslerror: (8, 'EOF occurred in violation of protocol')

Tomas Brabenec 18 years, 5 months ago  # | flag

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:

Traceback (most recent call last):
  File "", line 1, in ?
  File "C:\Python\lib\site-packages\M2Crypto\__init__.py", line 7, in ?
    import __m2crypto
ImportError: No module named __m2crypto

I download M2Crypto from M2Crypto webpage, but __m2crypto.py not in build\lib.win32-2.3\M2Crypto directory.

Tomas Brabenec 18 years, 5 months ago  # | flag

(8, 'EOF occurred in violation of protocol'). Hello all, problem with this error is here:

proxy.sendall(proxy_pieces+'\r\n')

This is fixed:

proxy.sendall(proxy_pieces)

Problem is in three end of lines in request. The request must include only two end of lines.

John Nielsen (author) 18 years, 3 months ago  # | flag

I removed redundant \r\n. I removed that small bug, thanks for noticing it.

Vlad Ender 18 years ago  # | flag

The problem. Would you care to post your solution? Or, alternatively, email me? Thanks, Vlad

Scott Melton 16 years, 7 months ago  # | flag

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.