-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxy.py
65 lines (54 loc) · 1.74 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from email import header
from types import MethodDescriptorType
from flask import Flask, request
import json5 as json
import traceback
import requests
# 初始化flask框架
app = Flask(__name__)
# 用于客户端进行HTTP交互的端口
@app.route('/', methods=['POST'])
def root():
try:
method: str = request.form['method'].upper()
url: str = request.form['url']
api_key = json.loads(request.form['api_key'])
headers = {
'X-MBX-APIKEY': api_key
}
if method == 'POST':
response = requests.post(url, headers=headers)
elif method == 'GET':
response = requests.get(url, headers=headers)
else:
raise('method必须为POST或者为GET')
if response.status_code == 200:
return json.dumps({
'msg': 'success',
'data': response.text
})
else:
return json.dumps({
'msg': 'error',
'data': response.text
})
except:
return json.dumps({
'msg': 'error',
'traceback': traceback.format_exc(),
})
if __name__ == '__main__':
# 读取配置文件
with open('config.json', 'r', encoding='utf-8') as f:
config = json.loads(f.read())
print('检查配置文件是否有效...', end='')
if 'port' in config and isinstance(config['port'], int) and \
0 <= config['port'] <= 65535:
print('OK')
else:
print('ERROR')
raise('配置文件无效,请检查port输入是否正确')
ip = '0.0.0.0'
port = config['port']
print('即将运行http服务器{}:{}'.format(ip, port))
app.run(ip, port, debug=False, ssl_context='adhoc')