4

I'm trying define a proxy handler to use http.client behind a proxy company. I know just how to use or define a proxy handler to urllib.:

http_proxy_full_auth_string = "http://"+"%s:%s@%s:%s" % (http_proxy_user,
                        http_proxy_passwd,
                        http_proxy_server,
                        http_proxy_port)
proxy_handler = urllib.request.ProxyHandler({"http": http_proxy_full_auth_string})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
resp = urllib.request.urlopen(uri).read()

And using http.client...?

P.S: sorry for the low english skills...

2 Answers 2

3

See the httplib python 3 documentation

import http.client
conn = http.client.HTTPSConnection("proxy_domain", 8080)
conn.set_tunnel("www.python.org")
conn.request("HEAD","/index.html")
Sign up to request clarification or add additional context in comments.

1 Comment

how to set authenticated proxy?
3

This might be old thread but folks may stumble upon it like I did and dont know how to authenticate.

import http.client
import base64
auth_hash = base64.b64encode(b"username:password").decode("utf-8")

conn = http.client.HTTPSConnection("proxy-ip or hostname", port="proxy-port")
conn.set_tunnel(
  "example.com", 
  headers={"Proxy-Authorization": f"Basic {auth_hash}"})

conn.request("GET", "/")

This is how you do it with basic authentication.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.