记录python的学习路程
实现HTTP-Basic认证
#!/usr/bin/python #coding:utf-8 #只限于python2.X import urllib2 import json def 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)
本文出自 “逢场做戏。” 博客,请务必保留此出处http://xiaofengmo.blog.51cto.com/10116365/1770355
原文地址:http://xiaofengmo.blog.51cto.com/10116365/1770355