记录python的学习路程

实现HTTP-Basic认证

#!/usr/bin/python#coding:utf-8#只限于python2.Ximport urllib2import jsondef httpbasic(url=None,username=None,password=None):    url = url    username = username    password = password    #后面有\n,需要去掉    s1 = base64.encodestring('{0}:{1}'.format(username, password))[:-1]     authheader =  "Basic {0}".format(s1)    res = urllib2.Request(url,{"Content-Type": "application/json","Authorization": authheader})    resp = urllib2.urlopen(res)    if __name__ == '__main__':    url = '    username = 'httpuser'    password = 'httppass'     httpbasic(url, username=username, password=password)