Created
March 23, 2022 12:25
-
-
Save 79man/146aa167d72865d560a500b64142319f to your computer and use it in GitHub Desktop.
Python3: use httplib with proxy authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client, base64, sys, json | |
if len(sys.argv) < 2 : | |
print("Usage: " + sys.argv[0] + " username password"); | |
quit() | |
proxy_details = { | |
"url" : "xxx.xxx.xxx.xxx", # replace with correct URI | |
"port" : 8080, # replace with correct port | |
"username" : sys.argv[1], # From command line argument | |
"password" : sys.argv[2] # From command line argument | |
} | |
host_details = { | |
"url" : "a.b.c.d", # replace with correct URL | |
"port" : 443 #replace with correct port | |
} | |
# payload is optional. Use if the endpoint needs it | |
payload = json.dumps({ | |
"key-1": "val-1" | |
}) | |
headers = {} # add any headers the destination needs here | |
conn = http.client.HTTPSConnection(proxy_details['url'], proxy_details['port']) | |
auth = '%s:%s' % (proxy_details['username'], proxy_details['password']) | |
headers['Proxy-Authorization'] = 'Basic ' + str(base64.b64encode(auth.encode())).replace("b'", "").replace("'", "") | |
print(headers) | |
conn.set_tunnel(host_details['url'], host_details['port'], headers) | |
conn.request("POST", "/", payload, headers) # replace with correct HTTP parameters GET/POST/... and path | |
response = conn.getresponse() | |
print(response.status, response.reason) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment