随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

Python3通过cookie登录

 

复制代码
import json,urllib.request,urllib.parse,http.cookiejar

url_base = 'https://spam.forti.com/api/v1'
url_admin = 'AdminLogin'
data = {'name' : 'admin',
        'password' : 'password'
        }
headers = {'Content-Type':'application/json','User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
postdata = bytes(json.dumps(data),'utf8') #post json格式数据
print(postdata)
request = urllib.request.Request(url_base + '/' + url_admin,postdata,headers)

#将cookie内容保存到文件
cookie_file = 'E:\\test\\spam_cookie.txt'
cookie=http.cookiejar.MozillaCookieJar(cookie_file)
handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler)
#登录获取cookie
url_login = 'https://spam.yxqiche.com/api/v1/AdminLogin'
result=opener.open(request)
#保存cookie内容到txt文件
cookie.save(ignore_discard=True,ignore_expires=True)


#访问其他url获取数据
url_sys = 'https://spam.forti.com/api/v1/SysStatusSysinfo/'
res = opener.open(url_sys)
reponse = res.read()
content  = json.loads(reponse.decode('utf8'))
print(content)
复制代码

 

#如果提示ssl certificate_verify_failed,则为opener添加额外的handler,不检查ssl证书

#将cookie内容保存到文件
cookie_file = '/data1/scripts/spam_cookie.txt'
cookie=http.cookiejar.MozillaCookieJar(cookie_file)
#如果提示ssl certificate_verify_failed,则为opener添加额外的handler,不检查ssl证书
context = ssl._create_unverified_context()
https_handler = urllib.request.HTTPSHandler(context=context)
handler=urllib.request.HTTPCookieProcessor(cookie)
opener=urllib.request.build_opener(handler,https_handler)

 参考:https://www.cnblogs.com/piwefei/p/11188071.html

https://www.cnblogs.com/lincappu/p/12762771.html

https://cloud.tencent.com/developer/article/1742483

https://blog.csdn.net/weixin_39867662/article/details/111427417

 

posted on   momingliu11  阅读(461)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-03-09 启用Netlogon debug,查看服务器验证瓶颈
2020-03-09 Zabbix按照时间段进行监控告警
2016-03-09 django导入/导出原始数据
2016-03-09 django定义Model中的方法和属性
2016-03-09 装饰器( decorate )
2016-03-09 django1.8 增加注册用户其他字段(用户扩展)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示