Python httplib.HTTPSConnection with proxy

5.2k views Asked by At

I would like to send a Get request from python's httplib library, using HTTPS protocol.

I have already went through multiple answers suggesting how to do it when using http protocol, but they didn't work with https.

here's my code to defile an HTTPSConnection.

connection = httplib.HTTPSConnection(server, port=port, context=ssl_context)
connection.request("GET", path, None, headers)

I haven't included any proxy server yet in this code. If someone could tell me how to send this request with a proxy server (say : x.x.x.x:8080), it would be great help.

Update 1: I tried the following code-

import httplib
ssl_context = ssl.create_default_context()
conn = httplib.HTTPSConnection("10.136.8.10", port=8080, context=ssl_context)
conn.set_tunnel("registry-1.docker.io", port=port)
conn.request("GET", "/", None, headers) 
resp = conn.getresponse()
print resp1.status

Still not working getting error 503.

1

There are 1 answers

3
Nurjan On

I haven't tried to connect to proxy through HTTPSConnection method, but it works with HTTPConnection. Any way, try to do this:

connection = httplib.HTTPSConnection(proxy_server, port=port, context=ssl_context)
connection.set_tunnel(url, port) # usually port is 443
connection.request("GET", "/", None, headers)