16

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?

Konstantin Rusanov
  • 6,414
  • 11
  • 42
  • 55

1 Answers1

28

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)
Konstantin Rusanov
  • 6,414
  • 11
  • 42
  • 55
  • 1
    Just for the further enlightenment of those who come along later - this is not an unknown issue in requests, see https://github.com/requests/requests/issues/879 Readers may want to investigate the solutions tried there. – Malik A. Rumi Dec 17 '17 at 20:30
  • Thank you for this solution, I banged my head for almost 3 days.... – PanDe Jan 19 '22 at 00:43