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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| from flask import Flask, request, jsonify import requests,time,json,re from lxml import etree
''' ---------------------------------------------- 启动命令: ---------------------------------------------- linux: 需要安装: pip install Flask gunicorn gunicorn -w 4 -b 0.0.0.0:8000 app:app 第一个app : 文件名 第二app 为文件中app对象 window: waitress-serve --listen=*:8000 app:app 需要安装: pip install Flask waitress 第一个app : 文件名 第二app 为文件中app对象 ---------------------------------------------- 一共5个接口(3个get接口,2个post) GET接口: 测试 ip:port 速度快: ip:port/get/test 速度慢: ip:port/get POST接口: 速度快: ip:port/post/test 速度慢: ip:port/post ---------------------------------------------- 注意: 除了测试的get接口, 其余需要在headers里加token参数 post请求需要添加请求体 ---------------------------------------------- 请求头demo:
md5 = hashlib.md5() md5.update('demo'.encode('utf-8')) headers={ 'token': md5.hexdigest(), 'Content-Type':'application/json' } ---------------------------------------------- 请求体demo:
data={ 't':int(time.time()) } ---------------------------------------------- '''
app = Flask(__name__)
def get_ansor(): res=requests.get('https://www.shiyebian.net/jiangsu/nanjing/').text HTML=etree.HTML(res) ul_list=HTML.xpath('//div[@class="main bg-white"]//ul') data = [] for li in ul_list: urls=li.xpath('./li/a/@href') titles=li.xpath('./li/a/text()') if len(urls) == len(titles): for i in range(len(urls)): data.append({'url': urls[i], 'title': titles[i].encode('latin-1').decode('GBK')}) else: print({'code':3000,"message":'error'}) return data
def num_to_chinese_units(num):
chinese_units = {'亿': 100000000, '万': 10000} result = [] remainder = num for unit, value in chinese_units.items(): if remainder >= value: quotient, remainder = divmod(remainder, value) result.append(f'{quotient:.2f}{unit}') if remainder != 0: result.append(f'{remainder:.2f}元') return ' '.join(result)
def get_skd(): t = int(time.time() * 1000) url = f'https://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112301302125560766778_{t}&pn=1&pz=500&po=1&np=1&fields=f12%2Cf13%2Cf14%2Cf62&fid=f62&fs=m%3A90%2Bt%3A2&ut=b2884a393a59ad64002292a3e90d46a5&_={t + 1}' res = requests.get(url,verify=False).text res_json = json.loads(re.findall(r'\((.*?)\)', res)[0]) data=[] for i in range(0, 6): disk = {} disk['name'] = res_json['data']['diff'][i]['f14'] disk['price'] = num_to_chinese_units(res_json['data']['diff'][i]['f62']) data.append(disk) return data
def validate_token(token): if len(token) ==32: return True else: return False
def token_required(f): def decorator(*args, **kwargs): token = request.headers.get('token') if not token: return jsonify({'message': 'Token is missing!'}), 401 if not validate_token(token): return jsonify({'message': 'Invalid token!'}), 403 return f(*args, **kwargs) decorator.__name__ = f.__name__ return decorator
@app.route('/get', methods=['GET']) @token_required def get_(): params=get_ansor() response_data = { 'message': 'ok',
'data':params } return jsonify(response_data), 200
@app.route('/get/test', methods=['GET']) @token_required def get_test(): response_data = { 'message': 'ok', 'data':'done' } return jsonify(response_data), 200 @app.route('/post/test', methods=['POST']) @token_required def post_(): print(request.data) if request.is_json:
response_data = { 'message': 'ok', 'data': 'done' } return jsonify(response_data), 200 else: return jsonify({'message': 'error'}), 400 @app.route('/post', methods=['POST']) @token_required def post_test(): print(request.data) if request.is_json: data = get_skd() response_data = { 'message': 'ok', 'data': data } return jsonify(response_data), 200 else: return jsonify({'message': 'error'}), 400
@app.route('/', methods=['GET']) def all(): return '这是locust 测试页'
if __name__ == '__main__': app.run()
|