TypeError:offer()缺少1个位置参数:“请求”

2024-05-18 06:12:06 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:在@Ajax1234回答之后

现在我在控制台和app.py env中发现了内部服务器错误

TypeError: offer() missing 1 positional argument: 'request'

我知道有很多类似的问题,但它们不适合我,所以长话短说,我用webRTC编写了quick app flask,它工作得很好,但当我试图将我的解决方案实现到现有项目时,我的代码不起作用。我以前没有烧瓶的经验,我只是被扔到那里做点什么

这项工作:

async def javascript(request):
    content = open(os.path.join(ROOT, "streaming.js"), "r").read()
    return web.Response(content_type="application/javascript", text=content)

async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

if __name__ == "__main__":
    app = web.Application()
    app.router.add_get("/", index)
    app.router.add_get("/streaming.js", javascript)
    app.router.add_post("/offer", offer)
    web.run_app(app)

使用这个js代码段

 var offer = pc.localDescription;
        return fetch('/offer', {
            body: JSON.stringify({
                sdp: offer.sdp,
                type: offer.type,
            }),
            headers: {
                'Content-Type': 'application/json'
            },
            method: 'POST'
        });

但是,当我用当前应用程序实现这段代码并添加app.route("/offer/something)时,它就是不起作用,老实说,我不知道为什么,因为我不知道

@app.route('/streaming.js/<request>', methods=['GET'])
async def javascript(request):
    content = open(os.path.join(ROOT, "streaming.js"), "r").read()
    print(content)
    return web.Response(content_type="application/javascript", text=content)

@app.route('/offer/<request>', methods=['POST'])
async def offer(request):
    params = await request.json()
    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])

我猜它不起作用,因为我的应用程序的结构将所有内容都放在同一个文件夹中,比如

  • app.py
  • streaming.js
  • index.html

但是我正在实现的这个应用程序有静态模板文件夹


Tags: webappsdpasyncreturnapplicationrequestdef
1条回答
网友
1楼 · 发布于 2024-05-18 06:12:06

您不需要Flask应用程序中路由的<request>组件。您的fetch代码段正在发送附加到请求的数据,Flask可以通过flask.request对象读取这些数据:

@app.route('/streaming.js', methods=['GET'])
async def javascript():
   content = open(os.path.join(ROOT, "streaming.js"), "r").read()
   print(content)
   return web.Response(content_type="application/javascript", text=content)

@app.route('/offer', methods=['POST'])
async def offer():
   #request.json retrieves the data attached to the post request
   params = await request.json()
   offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
   #do something with offer

相关问题 更多 >