Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read https url from Python with basic access authentication

How do you open https url in Python?

import urllib2

url = "https://user:[email protected]/path/
f = urllib2.urlopen(url)
print f.read()

gives:

httplib.InvalidURL: nonnumeric port: '[email protected]'
like image 426
Etam Avatar asked Nov 30 '22 19:11

Etam


1 Answers

This has never failed me

import urllib2, base64
username = 'foo'
password = 'bar'
auth_encoded = base64.encodestring('%s:%s' % (username, password))[:-1]

req = urllib2.Request('https://somewebsite.com')
req.add_header('Authorization', 'Basic %s' % auth_encoded)
try:
    response = urllib2.urlopen(req)
except urllib2.HTTPError, http_e:
    # etc...
    pass
like image 191
John Giotta Avatar answered Dec 08 '22 15:12

John Giotta