HTTP basic authentication with Python
import urllib2, base64, sys
 
username = "user_name"
password = "pass_word"
 
http_request = urllib2.Request("http://mywebsite.com/")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('n', '')
http_request.add_header("Authorization", "Basic %s" % base64string)   
 
try:
	result = urllib2.urlopen(http_request)
	if(result):
		print "i am in"
	else:
		print "username or password is wrong"
except IOError, e:
    print "username or password is wrong"
    sys.exit(1)
Categories: Python Tags: , , ,
%d bloggers like this: