Skip to content

Commit 49e4cdb

Browse files
author
caofanCPU
committed
httpie + shell auto script
1 parent 413947e commit 49e4cdb

File tree

11 files changed

+16645
-2
lines changed

11 files changed

+16645
-2
lines changed

auto/ID.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9290

auto/backup.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- 创建分类(/api/v100/live/employee/upload/dibblingVideo/addCategory)
2+
"name=默认分类",
3+
"schoolId=NONE"
4+
5+
- 登录
6+
"phone=18999999999",
7+
"smsAuthCode=123456",
8+
"loginType:=0",
9+
"pwd=ht123456.",
10+
11+
12+
- 删除分类(/api/v100/live/employee/upload/dibblingVideo/deleteCategory)
13+
"categoryId=NONE"

auto/batchHandler.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import json
4+
import ssl
5+
import sys
6+
import subprocess
7+
import time
8+
import pandas as pd
9+
10+
import requests
11+
12+
# 屏蔽HTTPS证书校验, 忽略安全警告
13+
requests.packages.urllib3.disable_warnings()
14+
context = ssl._create_unverified_context()
15+
joiner = ' '
16+
id_key = 'NONE'
17+
# 登陆一次后是否服复用cookie
18+
hot_reload = True
19+
cmd = "http"
20+
no_ca = "--verify=no"
21+
httpie_allow_view = {
22+
"-v": "显示请求详细信息",
23+
"-h": "显示请求头",
24+
"-b": "显示请求Body",
25+
"-d": "下载文件",
26+
"": "默认"
27+
}
28+
httpie_view = None
29+
try:
30+
if len(sys.argv) > 1:
31+
if httpie_allow_view.get(sys.argv[1]) is not None:
32+
httpie_view = sys.argv[1]
33+
else:
34+
print("输入参数有误, 仅支持如下参数: -v显示请求详细信息|-h显示请求头|-b显示请求Body|-d下载文件")
35+
except Exception as e:
36+
print(e)
37+
38+
39+
def httpie_cmd(id):
40+
"""
41+
执行excuteUrl.json接口
42+
:param id
43+
:return:
44+
"""
45+
with open("./excuteUrl.json", 'r') as request_data:
46+
request_json = json.load(request_data)
47+
url = request_json['url']
48+
method = request_json['method']
49+
request_headers = load_request_headers(request_json['headers'], hot_reload)
50+
headers = joiner.join(request_headers)
51+
body = joiner.join(request_json['body'])
52+
body = body.replace(id_key, str(id))
53+
httpie_params = [cmd, no_ca]
54+
if httpie_view is not None:
55+
httpie_params.append(httpie_view)
56+
httpie_params.extend([method, url, headers, body])
57+
httpie = joiner.join(httpie_params)
58+
print(httpie)
59+
print("当前ID: ", id)
60+
# 延时执行
61+
time.sleep(0.05)
62+
subprocess.call(httpie, shell=True)
63+
64+
65+
def load_request_headers(headers, hot_reload):
66+
"""
67+
加载请求header
68+
:param headers:
69+
:param hot_reload: 是否热加载, 复用已有Cookie
70+
:return:
71+
"""
72+
if hot_reload:
73+
with open("./cookie.txt", "r") as f:
74+
cookie = ''.join(f.readlines())
75+
else:
76+
cookie = auto_login()
77+
headers.append(cookie)
78+
return headers
79+
80+
81+
def load_data():
82+
"""
83+
读取数据文件, 每行为一条数据
84+
:return:
85+
"""
86+
data = pd.read_csv("./ID.csv", header=-1)
87+
data.columns = ['id']
88+
return data['id']
89+
90+
91+
def auto_login():
92+
"""
93+
自动登录, 获取登录Cookie, 写入文件, 在控制台打印
94+
"""
95+
with open("./ssoLogin.json", 'r') as sso_login_request_data:
96+
request_json = json.load(sso_login_request_data)
97+
url = request_json['url']
98+
method = request_json['method']
99+
request_headers = {}
100+
for item in request_json['headers']:
101+
split = item.replace('=', '').split(':')
102+
request_headers[split[0]] = split[1]
103+
request_body = {}
104+
for item in request_json['body']:
105+
split = item.replace(':', '').split('=')
106+
request_body[split[0]] = split[1]
107+
108+
request_headers = {"Content-Type": "application/json", "HT-app": "6"}
109+
response = requests.request(method, url, headers=request_headers, json=request_body, timeout=3, verify=False)
110+
response_headers = response.headers
111+
cookie = "'Cookie:" + response_headers.get("set-Cookie") + "'"
112+
with open("./cookie.txt", "w") as f:
113+
f.write(cookie)
114+
# JSON标准格式
115+
response_body = json.dumps(response.json(), ensure_ascii=False, indent=4)
116+
print(response_body)
117+
return cookie
118+
119+
120+
def main():
121+
# 首先登陆一次
122+
auto_login()
123+
for id in load_data():
124+
httpie_cmd(id)
125+
126+
127+
if __name__ == '__main__':
128+
main()

auto/cookie.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'Cookie:_r=ihJwwY0cYz49sr6PMLsFz0BZpRyQxkFQVg2iP9QJYR2ggATr7qhOvPeiiuQ5ZE9t|32ziSuOPkzw=; Max-Age=2592000; Expires=Fri, 08-May-2020 10:04:29 GMT; Domain=testa.huitong.com; Path=/; HttpOnly, _a=HUQD4RRBySY9sr6PMLsFz0BZpRyQxkFQVg2iP9QJYR2ggATr7qhOvCgr4ayK7PjY55U6X+1Po30u3aMceLb+8kFgGfrroYoW|6tzip/GZSQY=; Max-Age=7200; Expires=Wed, 08-Apr-2020 12:04:29 GMT; Domain=emp.testa.huitong.com; Path=/; HttpOnly, _a=HUQD4RRBySY9sr6PMLsFz0BZpRyQxkFQVg2iP9QJYR2ggATr7qhOvCgr4ayK7PjY55U6X+1Po30u3aMceLb+8kFgGfrroYoW|6tzip/GZSQY=; Max-Age=7200; Expires=Wed, 08-Apr-2020 12:04:29 GMT; Domain=emp2.testa.huitong.com; Path=/; HttpOnly'

auto/excuteUrl.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"url": "https://live.testa.huitong.com/api/v100/live/employee/upload/dibblingVideo/deleteCategory",
3+
"method": "POST",
4+
"headers" : [
5+
"Content-Type:application/json",
6+
"HT-app:6"
7+
],
8+
"body": [
9+
"categoryId=NONE"
10+
]
11+
}

0 commit comments

Comments
 (0)