Python 3.x “如何转储http”;内容类型:application/json&引用;在FastAPI中

Python 3.x “如何转储http”;内容类型:application/json&引用;在FastAPI中,python-3.x,http-headers,netcat,fastapi,Python 3.x,Http Headers,Netcat,Fastapi,我将编写一个python脚本,用于监听webhook中的自定义工具,该工具将在我可以指定的端口上发送json(它们可能还支持其他格式) 如何编写类似于linux的命令:“nc-l9000”来转储我在该端口上得到的输出(头和体) 从fastapi导入fastapi app=FastAPI() @app.get(“/”) 异步def root(): print()所以我认为您正在寻找请求 这包含了很多关于请求的信息(包括,主体、表单、标题等) from fastapi import FastAPI,

我将编写一个python脚本,用于监听webhook中的自定义工具,该工具将在我可以指定的端口上发送json(它们可能还支持其他格式)

如何编写类似于linux的命令:“nc-l9000”来转储我在该端口上得到的输出(头和体)

从fastapi导入fastapi
app=FastAPI()
@app.get(“/”)
异步def root():

print()所以我认为您正在寻找请求

这包含了很多关于请求的信息(包括,主体、表单、标题等)

from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/")
def read_root(request: Request):
    print(request.headers)
    return {}
现在,如果我向这个端点发送请求,它将返回我

{
    "host":"127.0.0.1:8000",
    "connection":"keep-alive",
    "accept":"application/json",
    "sec-fetch-site":"same-origin",
    "sec-fetch-mode":"cors",
    "sec-fetch-dest":"empty",
    "referer":"http://127.0.0.1:8000/docs",
    "accept-encoding":"gzip, deflate, br",
    "accept-language":"en-US,en;q=0.9,tr;q=0.8",
    "cookie":"csrftoken=sdf6ty78uewfıfehq7y8fuq; _ga=GA.1.11242141,1234423"
}
然后您可以从请求中提取accept.headers

如果你只需要这些,你也可以这样做

@app.get("/")
def read_root(request: Request):
    print(request.headers['accept'])
    return {}
这只会回来

Out: application/json

如果您指的是如何请求HTTP(S)页面,那么您可能正在寻找可能是或否,或者我希望转储进入端口的内容。web浏览器只是一个例子。我希望如何能够转储其他协议的其他请求,而不是HTTP?我认为这是不可能的,因为uvicorn是一个HTTP服务器。也许它前面的代理可以做这种工作。否则我不知道如何帮助OK,但可以转储原始http(s)请求?您可以使用中间件处理请求对象,请参阅和。不过,使用python很难获得原始请求。也许有一个库可以接收请求对象并对其进行转换,但我从未听说过它
Out: application/json