客服端:
#!/usr/bin/env python # encoding: utf-8 import requests import json,time def post_results(Download_link,Time): params = { 'download_link':Download_link, 'time':Time } print(params) r = requests.post('http://127.0.0.1:8000/subcontract', params=params) json_obj = json.loads(r.text) print(json_obj) def get_results(Download_link,Time): params = { 'download_link':Download_link, 'time':Time } print(params) r = requests.get('http://127.0.0.1:8000/subcontract', params=params) json_obj = json.loads(r.text) print(json_obj) if __name__ == '__main__': _time=int(time.time()) post_results('http://pkg.superdalan.com/game.pkg/download?pkgId=2990&f=chwyqmdmxttiOS_dalan_xy_dsdk_74_1.0.0_20200710_134530.apk',_time) get_results('http://pkg.superdalan.com/game.pkg/download?pkgId=2990&f=chwyqmdmxttiOS_dalan_xy_dsdk_74_1.0.0_20200710_134530.apk',_time)
服务端:
#!/usr/bin/env python # encoding: utf-8 import flask,json from flask import request, jsonify # 想获取到请求参数的话,就得用这个 server = flask.Flask(__name__) # 把这个python文件当做一个web服务 # 响应 def respond(flag, code, _method,msg=''): ret = { 'code': code, 'status': flag, 'msg': msg, 'method':_method } server.logger.info(ret)#日志记录响应结果 return jsonify(ret) @server.route('/subcontract', methods=['post','get']) # 接口的路径 def add_subcontract(): print('获取请求头',request.headers)#获取请求headers print('获取请求方式',request.method) print('获取请求cookes',request.cookies) print(request.headers.get("User-Agent"))##获取头部信息中User-Agent信息 print('获取URL',request.url)#包含主机+接口名称 print('获取接口名称',request.path) print('获取服务端ip:端口',request.url_root) if request.method=='POST': ##通过json提交 if bool((request.args).to_dict()): print('获取json全部请求参数:',request.args)#获取json请求参数 print('json请求参数转字典:',(request.args).to_dict()) print('获取指定参数的值',request.values.get('time'))#获取指定的请求参数值 print('获取json指定key的值',request.args['download_link']) print('指定请求参数',request.args.get("time")) ##普通的key/value的形式以表单form提交 elif bool((request.form).to_dict()): print('指定请求参数',request.values.get('time')) print('指定请求参数',request.form.get('time')) print('获取表单全部请求参数',request.form) print('获取表单全部请求参数转字典',(request.form).to_dict()) print('获取表单全部请求参数转字典',request.values.to_dict()) return respond(True, 200, 'POST',msg="渠道包上传成功!") elif request.method=='GET': print('获取get全部参数',request.args) print('获取get请求参数转字典',request.args.to_dict()) print('获取get指定参数',request.args.get('download_link')) ''' request.args.get("key") 获取get请求参数 request.values.get("key") 获取所有参数 ''' return respond(True, 200, 'GET',msg="渠道包上传成功!") if __name__ == '__main__': server.run(port=8000, debug=True, host='0.0.0.0')