Python requests return 504 in localhost

Konstantin Rusanov picture Konstantin Rusanov · Apr 6, 2017 · Viewed 18.9k times · Source

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.

My code:

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

Response error:

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
  • Connection refused
\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'

Flask code:

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'
....

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

Answer

Konstantin Rusanov picture Konstantin Rusanov · Apr 6, 2017

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)