首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3模拟登录zabbix

python3模拟登录zabbix

作者头像
py3study
发布2020-01-03 16:26:54
8780
发布2020-01-03 16:26:54
举报
文章被收录于专栏:python3python3python3
#!/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)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档