python请求服务器数据,验证用户名和密码,除了常用的HTTPBasicAuthHandler,还有HTTPDigestAuthHandler,可以通过fiddler抓包,查看服务器返回数据中的验证字段,如果是Digest验证,返回的格式的应该是
HTTP/1.1 401 Unauthorized
Connection: close
WWW-Authenticate: Digest qop="auth", realm="example", nonce="1444807486_3232235838"
import urllib
import urllib2
url_top = "http://www.example.com"
url = "http://www.example.com/index.html"
auth = urllib2.HTTPDigestAuthHandler() #根据服务器要求的验证方法,选择对应的handler
auth.add_password(realm,url_top,username,password)
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)
res_data = urllib2.urlopen(url)
res = res_data.read()
print res
参考:
http://www.th7.cn/Program/Python/201411/310230.shtml
https://zindilis.com/blog/2012/08/15/login-in-to-website-with-digest-authentication-in-python.html
http://blog.163.com/hlz_2599/blog/static/1423784742013415101252410/
Basic验证和Digest验证的设置方式