Created
January 31, 2016 05:33
-
-
Save tracyhatemice/fa9f38e4ecaad737288f to your computer and use it in GitHub Desktop.
python http auth module
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
from http.client import HTTPSConnection | |
from base64 import b64encode | |
#This sets up the https connection | |
c = HTTPSConnection("example.org") | |
#we need to base 64 encode it | |
#and then decode it to acsii as python 3 stores it as a byte string | |
userAndPass = b64encode(b"username:password").decode("ascii") | |
headers = { 'Authorization' : 'Basic %s' % userAndPass } | |
#then connect | |
c.request('GET', '/', headers=headers) | |
#get the response back | |
res = c.getresponse() | |
# at this point you could check the status etc | |
# this gets the page text | |
data = res.read() | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment