2017-10-17 33 views
1

我嘗試ping我Elasticsearch實例(通過盆景部署在Heroku上添加上)。我按照他們的指引,並嘗試對我的計算機上執行以下代碼:Accesing elasticsearch在Heroku盆景從我的電腦

from elasticsearch import Elasticsearch 
from settings import BONSAI_URL 
import re, logging 

# Log transport details (optional): 
logging.basicConfig(level=logging.INFO) 

# Parse the auth and host from env: 
bonsai = BONSAI_URL 
print(bonsai) 
auth = re.search('https\:\/\/(.*)\@', bonsai).group(1).split(':') 
host = bonsai.replace('https://%s:%[email protected]' % (auth[0], auth[1]), '') 

# Connect to cluster over SSL using auth for best security: 
es_header = [{ 
    'host': host, 
    'port': 443, 
    'use_ssl': True, 
    'http_auth': (auth[0],auth[1]) 
}] 

# Instantiate the new Elasticsearch connection: 
es = Elasticsearch(es_header) 

# Verify that Python can talk to Bonsai (optional): 
es.ping() 

我已經得到了以下錯誤消息:

elasticsearch.exceptions.ImproperlyConfigured: Root certificates are missing for certificate validation. Either pass them in using the ca_certs parameter or install certifi to use it automatically. 

我認爲這是錯誤的事實來,我沒有一個HTTPS證書,所以我用HTTP,通過刪除URL和正則表達式的s和切換use_ssl爲False,但我得到了以下錯誤:

urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')) 

如何將我的電腦中的數據插入到Heroku的elasticsearch中?

回答

1

您可能正在使用Python3。 問題是關於您的Python版本和urlib的行爲方式。

速戰速決可能是:

es_header = [{ 
'host': host, 
'port': 443, 
'use_ssl': True, 
'http_auth': (auth[0],auth[1]), 
'verify_certs': False 
}] 

但這種方式並不安全。更確切的修復可能是寫在你的requirements.txt:

certifi 

輸入您的終端:

pip install -r requirements.txt 

在您的文件你在哪裏instanciating elasticsearch:

import certifi 

然後啓動與之前啓動的代碼完全相同的代碼,它應該可以工作並且會很安全。

1

的問題是,客戶端無法找到根證書(這些生活在電腦上,你正在運行的代碼)。作爲例外提到,你應該能夠安裝certifipip,然後就import certifi在你的腳本,它應該沒有問題as described here運行。