需要批量执行需要登录的网站的某个http接口,自己写了python脚本去进行执行。要获取登录状态,可以使用谷歌浏览器F12打开开发者工具,在网络/network里面,在实际请求的地址上右键,然后Copy-> Copy as cURL。
这时复制出来的curl地址是可以直接在terminal里面直接执行的,其中cookie等记录登录状态信息的都有。
curl 'http://www.shutdown.cn/manage/quartz/resume.do?name=10.111.222.33&group=server-10.111.222.33' \
-H 'Connection: keep-alive' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' \
-H 'Cookie: route=7736bbdcb5ae01803cccfe9aa7852299; JSESSIONID=83B9C240FA66B95AFC4152EBFB15FDBB; ' \
--compressed \
--insecure
通过工具网站 https://curlconverter.com/#python 可以将curl地址转换成Python的request请求语句。
import requests
cookies = {
'route': '7736bbdcb5ae01803cccfe9aa7852299',
'JSESSIONID': '83B9C240FA66B95AFC4152EBFB15FDBB',
}
headers = {
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
params = (
('name', '10.111.222.33'),
('group', 'server-10.111.222.33'),
)
response = requests.get('http://www.shutdown.cn/manage/quartz/resume.do', headers=headers, params=params, cookies=cookies, verify=False)
这个语句直接复制到 python里面,处理成批量格式,然后直接执行就可以了