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 ...
随机推荐
- build gradle dependencies闭包的详解
转 :https://blog.csdn.net/guanguanboy/article/details/91043641 dependencies闭包的整体功能是指定当前项目所有依赖关系:本地依赖. ...
- [转帖]ps 命令详解
ps 命令详解 https://www.jianshu.com/p/cba22cce2f97 ps 概述 Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那 ...
- Java计算工作日的工具类
有时候需要根据工作日计算指定的日期,也就是需要排除周六日. 1. 初版代码如下: package cn.xm.exam.utils; import java.util.Calendar; impor ...
- 【题解】最长递增路径 [51nod1274]
[题解]最长递增路径 [51nod1274] 传送门:最长递增路径 \([51nod1274]\) [题目描述] 一个可能有自环有重边的无向图,每条边都有边权.输入两个整数 \(n,m\) 表示一共 ...
- PowerBI开发 第四篇:DAX 表达式基础
DAX 表达式主要用于创建度量列(Measure),度量值是根据用户选择的Filter和公式,计算聚合值,DAX表达式基本上都是引用对应的函数,函数的执行有表级(Table-Level)上下文和行级( ...
- SQL系列(三)—— 注释(comment)
SQL语句是由DBMS处理的指令.如果你希望包括不进行处理和执行的文本,该怎么办呢?为什么你想要这么做呢?原因有以下几 点. 我们这里使用的SQL语句都很短,也很简单.然而,随着你的SQL语句变长,复 ...
- selenium中元素操作之上传操作(六)
上传操作分为两种情况: 1.input标签上传 如果是input可以直接输入路径的,那么直接调用send_keys输入路径,和前边的元素操作类似,在这里不再过多的赘述. 2.非input标签上传 非i ...
- Git 解决合并分支时的冲突
参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/900004111093344 创建分支时,新分支的文件内容建立在原分支的基础上,我们称这时 ...
- ip2region.jar实现ip转地址
ip转地址 根据ip地址查询出所在地址. GitHub地址 https://github.com/lionsoul2014/ip2region/ pom坐标 <dependency> &l ...
- JavaScript数据类型和语法
第一章 类型 1.2 内置类型 使用 typeof 检测对象类型并不是非常安全的行为: // 安全的 typeof undefined // 'undefined' typeof true // 'b ...