python Basic Authentication

来源:互联网 发布:foxmail mac邮件位置 编辑:程序博客网 时间:2024/05/18 03:30

有时候我们需要python解析一些页面,实现自动化监控的功能。而这些页面一般需要用户输入username,password进行基本的验证,这时就需要我们使用python基本的认证功能。现已python抓取dubbo-admin的服务页面进行举例:

# create a password managerpassword_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()# Add the username and password.top_level_url = "http://1000.906.400.1620:88889/dubbo-admin/governance/services"username = "root"password = "testroot"password_mgr.add_password(None, top_level_url, username, password)handler = urllib2.HTTPBasicAuthHandler(password_mgr)# create "opener" (OpenerDirector instance)opener = urllib2.build_opener(handler)# use the opener to fetch a urlopener.open(top_level_url)# Install the openerurllib2.install_opener(opener)# Now all calls to use urllib2.urlopen use our opener.print urllib2.urlopen(top_level_url).read()

说明:这种认证解决了访问一个网页需要输入用户名、密码的认证,却没有专门的login请求的页面的认证方式。(不像qzone, 人人等都有一个类似login.do的post请求)。

0 0