【问题标题】:python localhost request on same localhost with different port在具有不同端口的同一 localhost 上的 python localhost 请求
【发布时间】:2017-06-28 14:39:28
【问题描述】:

我在 localhost (http://localhost:5000) 中使用 Flash 并尝试使用 python 请求从 elasticsearch 请求 json 结果

这是我的代码

@app.route('/test')
def TestElasticsearch():
  url = "http://localhost:9200/customer/_search?q=James&size=5"
  r = requests.get(url)
  print r
  return r.text

当我通过谷歌浏览器访问http://localhost:5000/test时,它返回

ConnectionError: HTTPConnectionPool(host='localhost', port=9200): Max retries exceeded with url: /customer/_search?q=James&size=5 (Caused by <class 'socket.error'>: [Errno 111] Connection refused)

请注意,我已尝试通过 google chrome 访问 http://localhost:9200/customer/_search?q=James&amp;size=5,它可以正常工作。

请帮我解决问题

【问题讨论】:

  • 尝试使用 127.0.0.1 而不是 localhost。

标签: python elasticsearch localhost


【解决方案1】:
Try:

@app.route('/test')
def TestElasticsearch():
    import urllib
    url = 'http://localhost:9200/customer/_search?q=James&size=5'
    # GET is the default action
    response = urllib.urlopen(url)  
    # Output from the GET assuming response code was 200
    r = response.read()   
    print r

【讨论】:

  • 感谢您的回答。但是,它不起作用。 IOError: [Errno socket error] [Errno 111] Connection refused
猜你喜欢
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 2012-06-07
  • 2012-05-08
  • 2020-07-06
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
相关资源
最近更新 更多