【问题标题】:Python requests return 504 in localhostPython 请求在 localhost 中返回 504
【发布时间】:2017-09-01 10:05:33
【问题描述】:

我运行 werkzeug 服务器(通过 Flask)并尝试通过 requests lib 连接到 localhost 并得到 504 错误,但如果在浏览器中打开 http://127.0.0.1:5000/ - 一切正常。

我的代码:

import requests
r = requests.get('http://127.0.0.1:5000/')
print(r.content)

响应错误:

b'\r\n\r\n错误:网关 超时\r\n

错误:网关超时

\r\n\r\n\r\n

在尝试检索 URL http://127.0.0.1:5000/ 时:

\r\n
  • 连接 拒绝
\r\n\r\n

您的缓存管理员是网站管理员。\r\n\r\n生成于 2017 年 4 月 6 日星期四 11:31:09 GMT 10.101.0.1 (Mikrotik HttpProxy)\r\n\r\n'

烧瓶代码:

from flask import Flask, request, render_template, jsonify, Response
import geoLocation
import models
import json
from bson.json_util import dumps
from bson import json_util



app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    return 'OK'
....

如何通过 requests lib 连接到 localhost (ip + port)?

【问题讨论】:

  • flask 应用的代码是什么?
  • 有问题的更新
  • 看来您开启了代理。
  • 你是如何发现它的以及我如何禁用它?
  • 你在linux上吗?你能运行:netstat -putan | grep 5000

标签: python timeout python-requests werkzeug


【解决方案1】:

找到解决方案,禁用代理

import os
import requests

os.environ['NO_PROXY'] = '127.0.0.1'
r = requests.get('http://127.0.0.1:5000')
print(r.content)

【讨论】:

  • 只是为了进一步启发后来者 - 这不是请求中的未知问题,请参阅github.com/requests/requests/issues/879 读者可能想调查那里尝试的解决方案。
  • 感谢您的解决方案,我头疼了将近 3 天......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2011-01-10
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多