python web 的 get 和 post 请求
python 实现 web接口,接收前端的 get 和 post 请求。
有 import web的写法,和 from flask import Flask, request 的写法
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # ============================================================================= # @Time : 2019/10/11/0011 14:52 # @Author : WanDaoYi # @FileName : hello_web.py # ============================================================================== import web import json class HelloWeb(object): def __init__(self): self.bill_model = "通用OCR" self.textAngle = "90" self.render = web.template.render('templates', base='base') pass def GET(self): data = web.input() data_len = len(data) if data_len == 0: print("no info") return {"oh": "you are wrong"} print("data_len: {}, data: {}".format(data_len, data)) bill_model = data.get("billModel", "") width = data.get("width", 0) height = data.get("height", 0) print("bill_model: {}, width: {}, height: {}".format(bill_model, width, height)) return {"ok": "nice"} pass def POST(self): data = web.data() data_json = json.loads(data) bill_model = data_json.get("billModel", "") text_angle = data_json.get("textAngle", False) print("bill_model: {}, text_angle: {}".format(bill_model, text_angle)) return {"code": "0000", "message": "", "ocrInfo": ["hello world", "ni hao"]} pass if __name__ == "__main__": # urls = (端口号后面的URL内容, 本文件的类名) # 通过 http://localhost:8080/do_test/ 可以请求 urls = ('/do_test/', 'HelloWeb',) app = web.application(urls, globals()) app.run() pass
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # ============================================================================= # @Time : 2019/10/10/0010 23:49 # @Author : WanDaoYi # @FileName : hello_flask.py # ============================================================================== from flask import Flask, request app = Flask(__name__) @app.route("/ocr_web/") def hello_world(): return "hello world!" @app.route("/list/") def my_list(): return "my list" @app.route("/dic/") def my_dic(): return {"user": "xiaochou"} @app.route("/get_text/") def get_text(): user = request.args.get("user", "") password = request.args.get("password", "") print("user: {}, password: {}".format(user, password)) return {"code": "get_text is ok"} # methods 默认为 GET 请求: methods=["GET"] @app.route("/post_text/", methods=['POST']) def post_text(): print("hello_post") input_param = request.get_json("") user = input_param.get("user", "") password = input_param.get("password", "") print("user: {}, password: {}".format(user, password)) res_info = {"code": "0000", "message": "", "ocrInfo": ["hello world", "ni hao"]} return res_info # return {"code": "post_text is ok"} if __name__ == "__main__": # 默认为 port 为 5000, 可以设置为8080 # 如果要debug,则可以设置 debug=True app.run(port=8080)
发起 get 请求
#!/usr/bin/env python # _*_ coding:utf-8 _*_ # ============================================================================= # @Time : 2019/10/10/0010 15:38 # @Author : WanDaoYi # @FileName : music_craw.py # ============================================================================== import requests # 文件保存路径 file_path = "./test_file/" music_name = "刘欢-重头再来" music_url = "music_url_info" def get_music(name, url): """ :param name: 歌名 :param url: 网页中打开一首音乐,打开开发者模式,点击播放,在下面出现的url中,找到类型为meidia的连接 url就是这个url参数。 :return: 歌曲 """ headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36' } resp = requests.get(url, headers=headers) with open(file_path + '{}.mp3'.format(name), 'wb') as f: f.write(resp.content) if __name__ == '__main__': get_music(music_name, music_url) print("Download is over!")