Python请求在本地主机中返回504 [英] Python requests return 504 in localhost

查看:48
本文介绍了Python请求在本地主机中返回504的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行 werkzeug 服务器(通过 Flask)并尝试通过请求库连接到本地主机并出现 504 错误,但如果打开 http://127.0.0.1:5000/ 在浏览器中 - 一切正常.

I run werkzeug server (via Flask) and trying to connect to localhost by requests lib and got 504 error, but if open http://127.0.0.1:5000/ in browser - anything ok.

我的代码:

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/:

b'\r\n\r\nERROR: Gateway Timeout\r\n

ERROR: Gateway Timeout

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

While trying to retrieve the URL http://127.0.0.1:5000/:

  • 连接被拒绝

您的缓存管理员是网站管理员.\r\n\r\n生成于格林威治标准时间 2017 年 4 月 6 日星期四 11:31:0910.101.0.1 (Mikrotik HttpProxy)\r\n\r\n'

Your cache administrator is webmaster.\r\n\r\nGenerated Thu, 06 Apr 2017 11:31:09 GMT by 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'
....

如何通过请求库连接到本地主机(ip + 端口)?

How to connect to localhost (ip + port) via requests lib?

推荐答案

找到解决方案,禁用代理

Found solution, disable proxy

import os
import requests

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

这篇关于Python请求在本地主机中返回504的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆