码迷,mamicode.com
首页 > 其他好文 > 详细

Requests 方法 -- post请求操作实践

时间:2019-07-06 19:43:30      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:正则表达式   打开   off   on()   首页   token   BMI   png   signed   

1、登录Jenkins抓包 ,小编的Jenkins部署在Tomcat上,把Jenkins.war 包放置到webapps目录。

本次用浏览器自带抓包,按下F12操作,主要看post就可以,登录是向服务器提交表单操作,则为post请求如下:

技术图片

 

2、body里面并不是json格式的,而是application/x-www-form-urlencoded,部分请求截图如下:

技术图片

 

3、实现登录代母如下:

import requests
# 先打开登录首页,获取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}

form_data = {
"j_username":" 自己的账号",
"j_password": "自己的密码",
"from": "/jenkins/",
"Submit": "登录",
"remember_me": "on"
}

r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 会话
# s = requests.session()
print(r.text)

4、返回的结果并不是json的格式,而是HTML格式,这里只截图部分的html结果

技术图片

 

5、也可以用正则表达式进行提取是否登录成功,这里截取HTML的两个关键字 ,代码如下:

import requests
# 先打开登录首页,获取部分 cookie
url1 = "http://localhost:8081/jenkins/j_acegi_security_check"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "screenResolution=1366x768; JSESSIONID=7B7F3F66948B1965ADDD332E5425A7F4; Pycharm-470d2d8b=7eb39a7e-c2b5-41ac-95f2-ee8fdf8f2188; jenkins-timestamper-offset=-28800000; csrftoken=qsBdlvEy1RoGOFiUqpvowtF9bkeP8XAXbZAhrZnOBqgwqmX3AA6TCxHFkAPSqBsN; hudson_auto_refresh=true; _ga=GA1.1.1092626020.1560260048; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTU2Mjc2NjkzMTQzODo1ODAxZTNhNDExMDlkODU1ZjFkZWIzZDg1ZGQxNWY4ZjA3NDExMmY2MDJlZDg4YzhlYmQ4YWZhYzMxYTc2OWY3; Pycharm-b245d0ed=4a1ebe68-e0eb-4b41-b28d-d0edee006e77",
"Origin": "http://localhost:8081",
"Referer": "http://localhost:8081/jenkins/login?from=%2Fjenkins%2F",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
}

form_data = {
"j_username":" 自己的账号",
"j_password": "自己的密码",
"from": "/jenkins/",
"Submit": "登录",
"remember_me": "on"
}

r = requests.post(url=url1,headers=headers,data=form_data,verify=False)
# session 会话
# s = requests.session()
print(r.text)

# 正则表达式提取账号和登录按钮
import re
t = re.findall("<b>(.+?)</b>",r.content.decode("utf-8")) #由于是编码格式,所以要进行decode解码
print(t[0])
print(t[1])

运行的结果为 1.自己的账号名 2.注销 。

6、复制出html页面到 http://www.kjson.com/xml/ 中进行解析下,有层次感。

技术图片

Requests 方法 -- post请求操作实践

标签:正则表达式   打开   off   on()   首页   token   BMI   png   signed   

原文地址:https://www.cnblogs.com/Teachertao/p/11143431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!