包括用户:通过elasticsearch在python scrip中上载

2024-04-18 22:03:25 发布

您现在位置:Python中文网/ 问答频道 /正文

当我向elasticsearch服务器发出PUT方法时,我在包含用户名和密码时遇到问题。脚本读取我的csv文件并将内容上传到ES。它可以正常工作,没有发出用户名和密码,但我似乎找不到一种方法,包括在httplib.HTTPConnection连接()

#url  = "10.100.1.16:9200"
key = "--user elastic:changeme"
url  = "10.100.9.21:9200"
path = "/" + args.elastic_path.strip("/")

print("Starting")
print(" CSV to ES ")
print("Importing %s rows into `%s` from '%s'" %(args.max_rows,args.elastic_path.strip("/"),args.csv_file))
print("")

count = 0
headers = []
with open(args.csv_file, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    for row in reader:
        if count == 0:
            for col in row:
                headers.append(col)
        elif count >= args.max_rows:
            exit('Max rows imported - exit')
        else:
            pos = 0
            _data = args.json_struct.replace("'",'"')
            _path = path
            for header in headers:
                _path = _path.replace('%'+header+'%',row[pos])
                _data = _data.replace('%'+header+'%',row[pos])
                pos += 1
            # Send the request
            connection = httplib.HTTPConnection(url)
            connection.request('PUT', _path, _data)
            response = connection.getresponse()
            print response.status, response.reason
            data = response.read()
            print data

        count += 1

exit('End')

我需要包括

--user elastic:changeme

我可以用pyelasticsearch完成这个任务,但是如果没有这个程序,我似乎无法继续前进

谢谢你的帮助


Tags: csvpathposurlfordataresponsecount
1条回答
网友
1楼 · 发布于 2024-04-18 22:03:25

首先,请注意您的问题:请添加python版本和库。另外,请去掉不需要的东西。 另外,由于您使用的是Security/Shield,我有点惊讶您没有使用https。 您可能需要考虑使用请求库。 但这应该是一个答案,所以它来了。在标题中包含用户名和密码,如下所示:

import httplib
from base64 import b64encode
url  = "127.0.0.1:9200"
userAndPass = b64encode("elastic:changeme")
connection = httplib.HTTPConnection(url)
headers = { 'Authorization' : 'Basic %s' %  userAndPass } 
connection.request('GET','/',headers=headers)
resp = connection.getresponse()
print resp.status, resp.reason

200行

resp.read()

'{\n“name”:“3paqSdV”、\n“cluster\u name”:“elasticsearch”、\n“cluster\u uuid”:“5MJosdgv-V7I6vIFb7AA”、\n“version”:{\n“number”:“5.6.0”、\n“build\u hash”:“781a835”、\n“build\u date”:“2017-09-07T03:09:58.087Z”、\n“build\u snapshot”:false、\n“lucene\u version”:“6.6.0”\n}、\n“tagline”:“您知道,用于搜索”\n}\n'

相关问题 更多 >