看了下python的urllib2模块,这里做个总结。
- import urllib2
- import cookielib
- import urllib
- class Hi_login:
- def __init__(self):
- cookie = cookielib.CookieJar()
- self.cookie = urllib2.HTTPCookieProcessor(cookie) ##### 生成cookie ###
- def login(self,user,pwd):
- url=''
- postdata=urllib.urlencode({
- 'mem_pass':'on',
-
- 'password':pwd
- 'Submit':'',
- 'tpl':'sp',
- 'tp_reg':'sp',
- 'u' :'',
- 'username':user})
- ### proxy_support = urllib2.ProxyHandler({"http":""}) 然后加入opener方法里####
- opener = urllib2.build_opener(self.cookie) ### 使用cookie ###
- headers = { ####### dict结构,可以加入x-forward-for甚至refer等 #######
- 'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
- urllib2.install_opener(opener)
- request = urllib2.Request(url,urllib.urlencode(postdata),headers = headers)
- urllib2.urlopen(request)
- if __name__=='__main__':
- pwd='123456'
- user='xiaofu'
- test=Hi_login()
- test.login(user,pwd)
假如访问需要认证的页面比如nagios监控页面等,
- import urllib2
- password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
- url = ""
- password_mgr.add_password(None, url, user='abc',passwd='xxxxxx')
- handler = urllib2.HTTPBasicAuthHandler(password_mgr)
- opener = urllib2.build_opener(handler)
- urllib2.install_opener(opener)
- f=urllib2.urlopen(url)
- print f.code
返回结果200,否则就是401认证错误
阅读(17555) | 评论(0) | 转发(1) |