urllib2: How to access a header from the SSL tunneling reply

378 Views Asked by At

I am using SSL tunneling with a proxy server to connect to a target server. I use http to connect to the proxy server and HTTPS to connect to the target server. The SSL tunneling works as it should and I can exchange HTTPS messages with the remote server, but there is a problem. The proxy server returns a header in its reply to urllib2's request to establish the SSL tunnel that I need to see, but I don't see a way to get access to it using urllib2 (Python 2.7.3).

I suppose I could theoretically implement the SSL tunneling handshake myself, but that would get me way deeper into the protocol than I want to be (or with which I feel comfortable).

Is there a way to get access to the reply using urllib2 when establishing the SSL tunnel?

UPDATE:

Here is the code that uses the proxy server to connect to the target server (the proxy server and the target server's URLs are not the actual ones):

proxy_handler = urllib2.ProxyHandler({'https': 'http://proxy.com'})
url_opener = urllib2.build_opener (proxy_handler)
request = urllib2.Request ('https://target_server.com/')
response = url_opener.open (request)
print response.headers.dict

I used WireShark to look at the message traffic. WireShark won't show me the bodies of the messages exchanged with the target server because they are encrypted, but I can see the body of the SSL Tunnel handshake. I can see the header that I'm interested coming back from the proxy server.

1

There are 1 best solutions below

2
On

How are you calling the https page.

are you using

resp = urllib2.urlopen('https')
resp.info().headers