python 访问需要HTTP Basic Authentication认证的资源
jopen
9年前
此方法用到了requests模块 #!/usr/bin/python # coding=utf8 import requests username = 'user'password = 'pass'url = 'http:// res = requests.get(url, auth=(username, password)) page = res.content print page
下面的这种方法是用urllib2模块
import urllib2 url = 'http:// username = 'user' password = 'pass' p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, url, username, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler)urllib2.install_opener(opener) page = urllib2.urlopen(url).read() print page
以上也参考了别人的东西,只是为了做一记录,只在http认证模块加密的内容验证过,别的还有待验证