简体   繁体   English

Python requests.post在本地主机上超时

[英]Python requests.post timeing out on localhost

I'm having trouble getting the Python requests module to post to an endpoint on the same server.我无法让 Python requests模块发布到同一服务器上的端点。

The server is running flask, and the route I want to post to is /new_applet .服务器正在运行flask,我想发布的路由是/new_applet

My code is below.我的代码如下。 It is inside a different flask route function, which could be an issue?它位于不同的烧瓶路由函数中,这可能是一个问题?

url = "http://0.0.0.0:5000/new_applet"
data = {
    "action": "add",
    "plugin": plugin,
    "version": version,
    "component": component
}

resp = requests.post(url, data=data)

However it hangs while trying to make the post request.但是,它在尝试发出发布请求时挂起。 Debugging shows that the request never reaches the flask route function.调试显示请求从未到达烧瓶路由功能。

The request works if I run the following command on the server:如果我在服务器上运行以下命令,则请求有效:

curl --location --request POST '0.0.0.0:5000/new_applet' \
  --form 'action=add' \
  --form 'plugin=up_spotify' \
  --form 'version=0.1' \
  --form 'component=inputs'

Why doesn't the Python request work, when the curl request does?curl请求有效时,为什么 Python 请求无效?

I believe flask runs a single thread by default.我相信烧瓶默认运行一个线程。

According to your explanation, if the request is made from flask then it blocks the thread waiting for a reply (same thread that is supposed to handle the request.post ).根据您的解释,如果请求是从烧瓶发出的,那么它会阻塞等待回复的线程(应该处理request.post线程)。

you could try to use multiple thread as explained here: Can I serve multiple clients using just Flask app.run() as standalone?您可以尝试使用多线程,如下所述: 我可以仅使用 Flask app.run() 作为独立服务器为多个客户端提供服务吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM