您的位置:首页 > 编程语言 > Python开发

python 登录验证

2015-10-14 15:42 555 查看
python请求服务器数据,验证用户名和密码,除了常用的HTTPBasicAuthHandler,还有HTTPDigestAuthHandler,可以通过fiddler抓包,查看服务器返回数据中的验证字段,如果是Digest验证,返回的格式的应该是

HTTP/1.1 401 Unauthorized
Connection: close
WWW-Authenticate: Digest qop="auth", realm="example", nonce="1444807486_3232235838"


python 请求数据的方法是:(使用urllib和urllib2)

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验证的设置方式

http://blog.jobbole.com/41519/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: