您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

python3发http请求,python如何发送http请求

来源:互联网 浏览:104次 时间:2023-04-08

一、python3写一个http接口服务,给别人调用3

????????这次选择fastapi,FastAPI是一个现代的、快速(高性能)的web框架,用于基于标准Python类型提示使用Python 3.6+构建api。具有快速、快速编码、更少的错误、直观、简单、简便、健壮。简易而且本地win10能够跑起来。

二、FastAPI的get接口代码实现

????????1.??安装:??pip install fastapi

? ? ? ? ? ? ? ? ? ? ? ?pip install?uvicorn

????????2.??代码:??

# !/usr/mtdmj/python# -*- coding: utf-8 -*-# @time : 2019/11/12 21:27# @author : Mo# @function: get service of fastapifrom fastapi import FastAPIapp = FastAPI()@app.get('/test/a={a}/b={b}')def calculate(a: int=None, b: int=None): c = a + b res = {"res":c} return resif __name__ == '__main__': import uvicorn uvicorn.run(app=app, host="0.0.0.0", port=8080, workers=1)

? ? ? ? 3.? 接口访问:http://127.0.0.1:8080/test/a=1/b=4

? ? ? ? ? ? ?浏览器访问结果:

? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ?postman访问结果:

? ? ? ? ? ? ? ? ? ? ?

三、FastAPI的post接口代码实现

????????1.??安装:??pip install fastapi

? ? ? ? ? ? ? ? ? ? ? ?pip install?uvicorn

????????2.??代码:??

# !/usr/mtdmj/python# -*- coding: utf-8 -*-# @time : 2019/11/12 21:27# @author : Mo# @function: post service of fastapifrom pydantic import BaseModelfrom fastapi import FastAPIapp = FastAPI()class Item(BaseModel): a: int = None b: int = None@app.post('/test')def calculate(request_data: Item): a = request_data.a b = request_data.b c = a + b res = {"res":c} return resif __name__ == '__main__': import uvicorn uvicorn.run(app=app, host="0.0.0.0", port=8080, workers=1)

? ? ? ? 3.? 接口访问:http://127.0.0.1:8080/test

? ? ? ? ? ? ?postman访问结果:

? ? ? ? ? ? ? ? ??

?

?

希望对你有所帮助!

10328836