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

python3模拟登录zabbix

2017-03-17 11:03 267 查看
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request
import http.cookiejar
import urllib.parse

# 登录的主页面

hosturl = 'http://xxxxx'  # 自己填写
# post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)
posturl = 'http://xxxxxxxxxxxxxxx/index.php'  # 从数据包中分析出,处理post请求的url

# 设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie
cj = http.cookiejar.LWPCookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
# 打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功)
h = urllib.request.urlopen(hosturl)

# 构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Referer': 'http://xxxxxxxxxxxx/index.php'}

# 构造Post数据,他也是从抓大的包里分析得出的。
postData = {"name": 'Admin',
"password": 'xxxxxxx',
"autologin": 1,
"enter": "Sign in"}

# 需要给Post数据编码

postData = urllib.parse.urlencode(postData).encode('utf-8')
# 通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程
request = urllib.request.Request(posturl, postData, headers)
response = urllib.request.urlopen(request)
text = response.read().decode()
print(text)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cookie python 处理器