http.client.HTTPSConnection and user authentication? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: http.client.HTTPSConnection and user authentication? (/thread-25558.html) |
http.client.HTTPSConnection and user authentication? - geekgeek - Apr-03-2020 We have a self-signed site I can use curl to retrieve the data using the REST API: curl https://user:[email protected]/rest/api/something But I don't know how to do the same using http.client.HTTPSConnection(). I did some google search but didn't see a good way. import http.client import ssl import urllib.parse context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations('mywonca.crt') conn = http.client.HTTPSConnection('username:[email protected]', 443, context=context) conn.request("GET","/rest/api/something") r1 = conn.getresponse()It complains the socket gaierror. I know I probably should not put the username:passws in the host part, but I don't know where I can set it. RE: http.client.HTTPSConnection and user authentication? - geekgeek - Apr-06-2020 I sorted it out myself. If someone is looking for the answer, here is the code: import http.client import ssl import ubase64 user="USERNAME" passwd="PASSWORD" headers = {"Authorization":"Basic {}".format(base64.b64encode(bytes(f"{user}:{passwd}","utf-8")).decode("ascii"))} context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations('mywonca.crt') conn = http.client.HTTPSConnection('somedomain.com', 443, context=context) conn.request("GET","/rest/api/something",headers=headers) r1 = conn.getresponse() RE: http.client.HTTPSConnection and user authentication? - 68k - Sep-20-2022 (Apr-06-2020, 01:27 AM)geekgeek Wrote: I sorted it out myself. If someone is looking for the answer, here is the code: Thanks! |