pythonpost与get请求参数_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!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值