python - 无法将JSON发布到ElasticSearch-未找到uri [\]和方法[POST]的处理程序

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

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

这是我的代码:

首先,我从mongoDB中提取数据,然后尝试将每个“文档”发布到我的 flex 文件中,地址是“http://127.0.0.1:9200”。我添加了一个额外的函数“my converter(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()

但是,当我访问我的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
#####
####
#####
#####

我尝试关注与处理相同问题的tome帖子,但对我无济于事。
知道为什么它不起作用吗?

最佳答案

有两个问题

  • url_base缺少索引和类型

    url_base ='http://127.0.0.1:9200/index/type'
  • headers必须是application/json(您尚未看到此代码,但是一旦解决了上述问题,您也会收到此错误。

    headers = {“content-type”:“application / json”}
  • 关于python - 无法将JSON发布到ElasticSearch-未找到uri [\]和方法[POST]的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846451/

    相关文章:

    python - 如何子类化 QMessageBox 并在关闭之前运行操作

    python - Elasticsearch,如何每天每小时获取订单

    PHP 脚本未将 Swift 数据插入 MYSQL 数据库

    post - 无法在 LISP hunchentoot 中获取帖子

    python - 字典python中的多个值

    python - 断言错误 : View function mapping is overwriting an existing endpoint function: main

    elasticsearch - 从Xpath创建嵌套字段并检查现有文档

    elasticsearch - 在ElasticSearch 5.2.1中从Groovy脚本迁移到轻松脚本

    iOS 通过 POST 到 Web 服务器 - 字符串被切断?

    Python:给定时区名称的所有可能时区缩写(反之亦然)