【发布时间】:2017-01-22 08:11:30
【问题描述】:
我正在从运行在 django+nginx+gunicorn 上的 Web 应用程序中的方法发送发布请求。在 django 自己的服务器(使用 runserver)上执行时,我没有收到来自相同代码的 200 响应的问题。
try:
response = requests.post(post_url, data=some_data)
if response.status_code == OK and response.content == '':
logger.info("Request successful")
else:
logger.info("Request failed with response({}): {}".format(response.status_code, response.content))
return response.status_code == OK and response.content == ''
except requests.RequestException as e:
logger.info("Request failed with exception: {}".format(e.message))
return False
我检查了 post_url 的服务器日志,它确实返回了 200 响应与此数据。但是,当我在 gunicorn 和 nginx 后面运行应用程序时,我无法接收响应(但是正在发送请求)。代码在 try 块后卡在第一行,gunicorn worker 超时(30 秒后)。
这是 post_url 处的 apache 服务器日志:
[14/Sep/2016:13:19:20 +0000] "POST POST_URL_PATH HTTP/1.0" 200 295 "-" "python-requests/2.9.1"
更新:
我忘了说,这个请求执行不到一秒,所以不是超时问题。配置有问题?我有标准的 nginx+gunicorn 设置,其中 gunicorn 设置为 nginx 中的 proxy_pass。我猜,因为我在 nginx 代理后面,我应该在从应用程序发送发布请求时做一些不同的事情吗?
【问题讨论】:
-
由于您提供了一些有用的诊断信息,我认为您可以在这里遇到{}类型的错误:-似乎您可以实现http请求但无法获得结果。首先,检查你的防火墙(可能是
iptables -L命令,看看你的INPUT表上是否有关于端口80 的规则)。 - 您的请求是对不可信服务器的 https 请求吗? - 您是否尝试过访问另一台服务器? -
你有多少个 gunicorn 工人?
-
@artragis 我可以使用 django shell 成功执行请求。我可以在同一台机器上在 python 脚本中运行相同的代码,而不会出现任何问题。
-
@VBart 我没有设置任何数字,这意味着它默认为 1。为了完成这个请求,另一个请求被发送回服务器......这让我觉得有更多的工人会解决这个问题吗?是的。