【问题标题】:Impossible to post JSON to ElasticSearch - No handler found for uri [\] and method [POST]无法将 JSON 发布到 ElasticSearch - 未找到 uri [\] 和方法 [POST] 的处理程序
【发布时间】:2018-12-19 07:28:19
【问题描述】:

我尝试将一些 JSON 格式的数据从 mongoDB 发布到 elasticSearch。

这是我的代码:

首先我从 mongoDB 中提取数据,然后我尝试将每个“文档”发布到我的弹性,地址是“http://127.0.0.1:9200”。我添加了一个额外的函数“我的转换器(o)”,以使 datetime 对象可序列化。

from pymongo import MongoClient
import requests
import json
import datetime

mongo_host = 'localhost'
mongo_port = '27017'

client=MongoClient(mongo_host+':'+mongo_port)
db = client.cvedb
collection=db['cves']

def myconverter(o):
     if isinstance(o, datetime.datetime):
            return o.__str__()



try: db.command("serverStatus")
except Exception as e: print(e)
else: print("You are connected!")
cursor = collection.find({})
for document in cursor:

    headers={"content-type":"application/x-www-form-urlencoded"}

    url_base = 'http://127.0.0.1:9200'

    data=document

    data_parsed=(json.dumps(data, default = myconverter))

    print("#####")
    print("#####")
    print(data_parsed)
    print("#####")
    print("#####")

    req = requests.post(url_base,json=data_parsed)
    print (req.status_code)
    print (req.text)

    print("####")
    print("#####")
    print (req.status_code)
    print("#####")
    print("####")
client.close()

但是当我 hcekc 我的 ES 时,在以下地址:“http://127.0.0.1:9200/_cat/indices”,什么都没有出现。这是我用终端得到的:

{"Modified": "2008-09-09 08:35:18.883000", "impact": {"confidentiality": "PARTIAL", "integrity": "PARTIAL", "availability": "PARTIAL"}, "summary": "KDE K-Mail allows local users to gain privileges via a symlink attack in temporary user directories.", "cvss": 4.6, "access": {"vector": "LOCAL", "authentication": "NONE", "complexity": "LOW"}, "vulnerable_configuration": ["cpe:2.3:a:kde:k-mail:1.1"], "_id": null, "references": ["http://www.redhat.com/support/errata/RHSA1999015_01.html", "http://www.securityfocus.com/bid/300"], "Published": "2000-01-04 00:00:00", "id": "CVE-1999-0735", "cvss-time": "2004-01-01 00:00:00", "vulnerable_configuration_cpe_2_2": ["cpe:/a:kde:k-mail:1.1"]}
#####
#####
400
No handler found for uri [/] and method [POST]
####
#####
400
#####
####
#####
#####

我尝试关注处理相同问题的巨著帖子,但对我没有任何帮助。 知道为什么它不起作用吗?

【问题讨论】:

    标签: python elasticsearch post httphandler http-status-code-400


    【解决方案1】:

    有两个问题

    1. url_base 缺少索引和类型

      url_base = 'http://127.0.0.1:9200/index/type'

    2. headers 必须是 application/json(你还没有看到这个,但是一旦你解决了上面的问题,你也会得到这个错误。

      headers={"content-type":"application/json"}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2023-03-28
      • 1970-01-01
      • 2018-05-26
      • 2016-12-13
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多