发送HTTP请求到本地主机

时间:2018-06-26 07:54:18

标签: python http elasticsearch flask ubuntu-18.04

我正在用Flask构建Rest API。该API使用从localhost运行的Elasticsearch数据。

是否可以从Flask路由向本地主机发送HTTP请求?

类似这样的东西:

@flashlog.route('/checkelasticisup')
def check_elastic_is_up():
    res = requests.get('http://localhost:9200/')
    return jsonify({'message': res.text})

我想将您将GET请求发送到localhost:9200时得到的Elasticsearch响应返回给客户端:

{
  "name": "wEV_Spx",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "dbPLLgYLRO69iYuT_pp4WA",
  "version": {
    "number": "6.3.0",
    "build_flavor": "default",
    "build_type": "deb",
    "build_hash": "424e937",
    "build_date": "2018-06-11T23:38:03.357887Z",
    "build_snapshot": false,
    "lucene_version": "7.3.1",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.0.0"
  },
  "tagline": "You Know, for Search"
}

现在,我得到:

  

网络错误(dns_unresolved_hostname)

<HTML><HEAD>
<TITLE>Network Error</TITLE>
</HEAD>
<BODY>
<FONT face="Helvetica">
<big><strong></strong></big><BR>
</FONT>
<blockquote>
<TABLE border=0 cellPadding=1 width="80%">
<TR><TD>
<FONT face="Helvetica">
<big>Network Error (dns_unresolved_hostname)</big>
<BR>
<BR>
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">
Your requested host "localhost" could not be resolved by DNS.
</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica">

</FONT>
</TD></TR>
<TR><TD>
<FONT face="Helvetica" SIZE=2>
<BR>
For assistance, contact your network support team.
</FONT>
</TD></TR>
</TABLE>
</blockquote>
</FONT>
</BODY></HTML>

如何将HTTP请求发送到本地主机(我正在使用Ubuntu 18.04 LTS)?
谢谢!

2 个答案:

答案 0 :(得分:0)

如@Metalik所说,您必须将localhost替换为127.0.0.1,这样才能正常工作。

答案 1 :(得分:0)

问题出在我的代理配置上。未配置要忽略的正确IP。 在我的Ubuntu机器的网络设置中将localhost,127.0.0.1 / 8和:: 1添加到ignore host字段后,一切正常。