python-自动登录禅道
from bs4 import BeautifulSoup
import hashlib
import requests
import re
from tool.request_data_change_to_StrEncode import request_data_change_to_str
import os
import json class zentao_login(object):
global file_login_info
file_login_info = './login_info.txt'#登录网页存放至本地,用于提取verifyRand def __init__(self):
self.data_to_str=request_data_change_to_str()
self.session = requests.session()
self.host = 'http://xxxx'
self.header={"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"} def md5_key(self,str):#md5加密
m = hashlib.md5()
b = str.encode(encoding='utf-8')
m.update(b)
return m.hexdigest() def login(self,user='admin',pw='sss',file=file_login_info):#登录
url=self.host+'/zentao/user-login.html'
content = self.session.get(url,headers = self.header)
with open(file,'w',encoding='utf-8') as file:#将网页保存到本地
file.write(content.text)
verifyRandstr=self.read_verifyRandstr()#获取登录字符串
password=self.md5_key(self.md5_key(pw)+verifyRandstr)#md5加密密码
login_url=self.host+'/zentao/user-login.html'
data=self.data_to_str.requestDataToStr_firefoxAndChrome('''account: admin
password: %s
passwordStrength: 1
referer: /zentao/
verifyRand: %s
keepLogin: 1'''%(password,verifyRandstr))
request_login=self.session.post(url=login_url,data=data,headers=self.header,allow_redirects=True)#登录
if "self.location='/zentao/" in request_login.text:
#判断登录是否成功,成功后将cookie写入文件
cookies = requests.utils.dict_from_cookiejar(request_login.cookies) #拿到所有的cookie
temp_str = ''#定义cookie字符串
# 获取sessionid,如果不保存至header,用requests直接请求会出错
session_str = 'zentaosid=' + request_login.cookies.values()[-1]
for key,value in cookies.items():
if (key=='zp' or key=='za' or key=='keepLogin'):
str='{}={};'.format(key,value)#拼接cookie,key/value
temp_str+=str#将拼接完成的cookie,添加至最终的cookie字符串
self.header['Cookie']=temp_str+session_str#将处理完成的cookie添加至请求header
with open('./zentao_header.txt','w',encoding='utf-8') as zentao_header:#将header写入至文件
zentao_header.write(repr(self.header))
elif "alert('登录失败,请检查您的用户名或密码是否填写正确。')" in request_login.text:
# 判断由于密码错误引起的登陆失败
raise Exception('密码错误,登录失败,请重试')
else:
# 判断由于其他情况引起的登录失败
raise Exception(request_login.text) def read_verifyRandstr(self,file=file_login_info):#匹配verifyRand
with open(file,'r',encoding='utf-8') as file_read:
login_html_info=file_read.read()
soup=BeautifulSoup(login_html_info,'lxml')
button=soup.find_all('input',attrs={'type': re.compile("hidden"),'id': re.compile("verifyRand")})[0]['value']
return button def load_header(self,user='admin',pw='test2019zentao',file='./zentao_header.txt'):
#测试cookie是否有效,有效的话就直接使用,无效就触发重新登录
'''这里遇到了一个问题,如果header里没有保存sessionid,那么用requests直接请求会出错,这时候需要用session请求才行,
如果header里保存了sessionid,那么用requests请求即可,当然用session请求也不会有问题,
所以最终还是把session保存进了header,与token认证还真不一样'''
try:#处理zentao_header.txt不存在的情况
with open(file,'r',encoding='utf-8') as header_info:
header=eval(header_info.read())
url_bug = self.host + '/zentao/bug-browse.json'
re_bug = requests.get(url=url_bug, headers=header)
if "self.location='/zentao/user-login-" in re_bug.text:
self.login(user, pw)
return header
except FileNotFoundError as error:
print(error,'执行登录')
self.login()
self.load_header() if __name__=="__main__":
zt=zentao_login()
# zt.login()
zt.load_header()
python-自动登录禅道的更多相关文章
- 用python实现自动化登录禅道系统 设置定时器自动执行脚本
由于各种原因,我想试下用python实现自动登录禅道系统,并且每天定时执行.(本人第一次接触自动化,在大佬眼中门槛都没摸到的类型) 首先缕清思路: 1.实现自动登录禅道系统,用selenium实现2. ...
- python+selenium自动化禅道登录测试
本文以禅道登录测试为例,思路主要分openBrowser.openUrl.findElement.sendVals.checkResult.ReadUserdate六部分 openBrowser de ...
- python自动登录代码
公司有很多管理平台,账号有禁用机制,每个月至少登录一次,否则禁用.导致有时候想登录某个平台的时候,发现账号已经被禁用了,还得走流程解禁.因此用python实现了一下自动登录,每天定时任务运行一次.ps ...
- 吴裕雄--天生自然PYTHON学习笔记:python自动登录网站
打开 www. 5 l eta . com 网站,如果己经通过某用户名进行了登录,那么先退出登录 . 登录该网站 的步骤一般如下 : ( 1 )单击右上角的“登录”按钮. ( 2 )先输入账号. ( ...
- 使用cookies,免密登录禅道(一)
导言:在做自动化的过程中,很多时候都需要绕过登录验证码来进行测试,可使用cookie 绕过验证码进行登录. 以下以自己搭建的禅道环境登录为例(其他网站也可以同样道理): #coding=gbkimpo ...
- Python 自动登录网站(处理Cookie)
http://digiter.iteye.com/blog/1300884 Python代码 def login(): cj = cookielib.CookieJar() ope ...
- Python 自动登录哔哩哔哩(2captcha打码平台)
前言 研究爬虫的各位小伙伴都知道,需要登录才能获取信息的网站,是比较难爬的,原因就是在于,现在各大网站为了反爬,都加入了图片验证码,滑动验证码之类的干扰 本篇就针对哔哩哔哩的滑动验证码进行讲解和破解 ...
- 5、Selenium+Python自动登录163邮箱发送邮件
1.Selenium实现自动化,需要定位元素,以下查看163邮箱的登录元素 (1)登录(定位到登录框,登录框是一个iframe,如果没有定位到iframe,是无法定位到账号框与密码框) 定位到邮箱框( ...
- jmeter登录禅道案例
下载jmeter,配置环境变量 变量名:JMETER_HOME 变量值:C:\Program Files\apache-jmeter-2.11 变量名:CLASSPATH 变量值:%JMETER_HO ...
随机推荐
- 文本分类(TextCNN,Keras)
数据集是网上找的,已上传至我的 Github,项目完整地址:https://github.com/cyandn/practice/tree/master/text-classification 流程: ...
- 贪心 --- Y2K Accounting Bug
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9691 Accepted: 483 ...
- CLH lock 原理及JAVA实现
--喜欢记得关注我哟[shoshana]-- 前记 JUC中的Lock中最核心的类AQS,其中AQS使用到了CLH队列的变种,故来研究一下CLH队列的原理及JAVA实现 一. CLH背景知识 SMP ...
- Centos Docker 安装 Apache Guacamole
经常在ubuntu上折腾,偶尔在centos来也来玩一把吧 1.安装DOCKER cd /etc/yum.repos.d wget https://download.docker.com/linux/ ...
- HDU校赛 | 2019 Multi-University Training Contest 3
2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 100 ...
- http://www.cnblogs.com/xdp-gacl/p/4200090.html
孤傲苍狼 只为成功找方法,不为失败找借口! JavaWeb学习总结(五十)——文件上传和下载 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功 ...
- Introducing KSQL: Streaming SQL for Apache Kafka
Update: KSQL is now available as a component of the Confluent Platform. I’m really excited to announ ...
- Echarts 学习系列(1)-5分钟上手ECharts
目录 写在前面 下载Echarts和主题 绘制一个简单的图表 写在前面 最近,在做某个项目的时候.需要使用的可视化的图表显数据.最后,选择了百度的Echarts. 下载Echarts和主题 1.获取E ...
- C#读取Excel文件,准换为list
经常会用到,废话不多说直接贴代码 //读取Excel文件 public static DataTable ReadExcelToTable(string path)//excel存放的路径{try{ ...
- 小tips:JS/CSS实现字符串单词首字母大写
css实现: text-transform:capitalize; JS代码一: String.prototype.firstUpperCase = function(){ return this.r ...