本文整理汇总了Python中elasticsearch_dsl.connections.connections.create_connection方法的典型用法代码示例。如果您正苦于以下问题:Python connections.create_connection方法的具体用法?Python connections.create_connection怎么用?Python connections.create_connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch_dsl.connections.connections
的用法示例。
在下文中一共展示了connections.create_connection方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_connection
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def prepare_connection():
"""Set dafault connection for ElasticSearch.
.. warning::
In case of using multiprocessing/multithreading, connection will
be probably initialized in the main process/thread and the same
connection (socket) will be used in all processes/threads. This
will cause some unexpected timeouts of pushes to Elasticsearch.
So make sure that this function is called again in each
process/thread to make sure that unique connection will be used.
"""
elasticsearch_host = getattr(settings, "ELASTICSEARCH_HOST", "localhost")
elasticsearch_port = getattr(settings, "ELASTICSEARCH_PORT", 9200)
connections.create_connection(
hosts=["{}:{}".format(elasticsearch_host, elasticsearch_port)]
)
示例2: applyConfig
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def applyConfig(self):
try:
print("Connecting to '%s', index '%s'" % (self.confESHost, self.confESIndex))
self.es = connections.create_connection(hosts=[self.confESHost])
self.idx = Index(self.confESIndex)
self.idx.doc_type(DocHTTPRequestResponse)
if self.idx.exists():
self.idx.open()
else:
self.idx.create()
self.callbacks.saveExtensionSetting("elasticburp.host", self.confESHost)
self.callbacks.saveExtensionSetting("elasticburp.index", self.confESIndex)
self.callbacks.saveExtensionSetting("elasticburp.tools", str(self.confBurpTools))
self.callbacks.saveExtensionSetting("elasticburp.onlyresp", str(int(self.confBurpOnlyResp)))
except Exception as e:
JOptionPane.showMessageDialog(self.panel, "<html><p style='width: 300px'>Error while initializing ElasticSearch: %s</p></html>" % (str(e)), "Error", JOptionPane.ERROR_MESSAGE)
### ITab ###
示例3: __enter__
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def __enter__(self):
connection = connections.create_connection(
hosts=['{}:{}'.format(
self.ini.read('ELASTICSEARCH', 'HOST'),
self.ini.read('ELASTICSEARCH', 'PORT')
)],
http_auth="{}:{}".format(
self.ini.read('ELASTICSEARCH', 'USERNAME'),
self.ini.read('ELASTICSEARCH', 'PASSWORD')
),
)
# create when index not exist
if not Webpage._index.exists():
Webpage._index.create()
if not Port._index.exists():
Port._index.create()
return connection
示例4: restore_tokens
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def restore_tokens():
connections.create_connection(hosts=ES_NODES)
Index(INDEX_NAME).delete()
class Token(DocType):
username = String()
token = String()
expires = Date()
read = Boolean()
write = Boolean()
revoked = Boolean()
acl = String()
groups = String()
admin = Boolean()
last_activity_at = Date()
class Meta:
index = INDEX_NAME
Token.init()
reindex_results = connections.get_connection().reindex(body={"source": {"index": BACKUP_INDEX_NAME}, "dest": {"index": INDEX_NAME}}, request_timeout=3600)
if reindex_results.get('created') + reindex_results.get('updated') == reindex_results.get('total'):
return ('Tokens restored to previous schema successfully!')
else:
return ('Tokens did not restore from backup properly')
示例5: __init__
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def __init__(self, game, call_every='step'):
connections.create_connection(hosts=['localhost'])
self.game = game
self.call_every = call_every
示例6: mitm_request
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def mitm_request(self, data):
# Initialize ES connection and index
res = connections.create_connection(hosts=[args.elasticsearch])
idx = Index(args.index)
idx.doc_type(DocHTTPRequestResponse)
try:
DocHTTPRequestResponse.init()
idx.create()
except:
pass
r = HTTPRequest(data)
# determine url
if self.is_connect:
scheme = "https"
else:
scheme = "http"
url = scheme + "://" + self.hostname
if scheme == "http" and int(self.port) != 80 or scheme == "https" and int(self.port) != 443:
url += ":" + str(self.port)
url += self.path
if args.verbose:
print(url)
self.doc = DocHTTPRequestResponse(host=self.hostname, port=int(self.port), protocol=scheme)
self.doc.meta.index = args.index
self.doc.request.url = url
self.doc.request.requestline = r.requestline
self.doc.request.method = r.command
self.doc.host = self.hostname
self.doc.port = int(self.port)
self.doc.protocol = scheme
return data
示例7: run
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def run(args):
"Collect data"
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, level=args.log_level)
# mute elasticsearch INFO logs
log = logging.getLogger('elasticsearch')
log.setLevel('ERROR')
# Define a default ElasticSearch client
es_hosts = [dict(host=args.es_host, port=args.es_port)]
connections.create_connection(hosts=es_hosts, timeout=30)
# Setup elastic search indices once before starting
setup_index_template()
if args.year:
years = args.year.split(',')
else:
years = year_headers.keys()
# Collect!
for year in years:
setup_index(year)
tse = TSE(year, path=args.download_directory)
tse.download_and_extract(remove_tmp_dir=False, remove_zip=False)
tse.all_candidates()
示例8: __init__
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def __init__(self, nodes=ES_NODES, **kwargs):
if type(nodes) == str:
nodes = nodes.split(',')
if not nodes:
nodes = ES_NODES
self.indicators_prefix = kwargs.get('indicators_prefix', 'indicators')
self.tokens_prefix = kwargs.get('tokens_prefix', 'tokens')
logger.info('setting es nodes {}'.format(nodes))
connections.create_connection(hosts=nodes)
self._alive = False
while not self._alive:
if not self._health_check():
logger.warn('ES cluster not accessible')
logger.info('retrying connection in 30s')
sleep(30)
self._alive = True
logger.info('ES connection successful')
self.tokens = TokenManager()
self.indicators = IndicatorManager()
示例9: reindex_tokens
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def reindex_tokens():
TokenBackup.init()
connections.create_connection(hosts=ES_NODES)
backup_results = connections.get_connection().reindex(body={"source": {"index": INDEX_NAME}, "dest": {"index": BACKUP_INDEX_NAME}}, request_timeout=3600)
if backup_results.get('created') + backup_results.get('updated') == backup_results.get('total'):
Index(INDEX_NAME).delete()
else:
return ('Tokens did not backup properly')
time.sleep(1)
Token.init()
reindex_results = connections.get_connection().reindex(body={"source": {"index": BACKUP_INDEX_NAME}, "dest": {"index": INDEX_NAME}}, request_timeout=3600)
if reindex_results.get('created') + reindex_results.get('updated') == reindex_results.get('total'):
return ('Tokens reindexed successfully!')
else:
return ('Tokens did not reindex from backup properly')
示例10: __init__
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def __init__(self, remote='localhost:9200', index='indicators', **kwargs):
super(_ElasticSearch, self).__init__(remote)
self.index = index
if isinstance(self.remote, str):
self.remote = self.remote.split(',')
connections.create_connection(hosts=self.remote)
示例11: _initialize
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def _initialize(self):
"""
Initialize a connection to an ES cluster and creates an index template if it does not exist.
"""
if not self._initialized:
http_auth = None
if self._access_key and self._secret_key and self._aws_region:
http_auth = AWS4Auth(self._access_key, self._secret_key, self._aws_region, "es")
elif self._access_key and self._secret_key:
http_auth = (self._access_key, self._secret_key)
else:
logger.warn("Connecting to Elasticsearch without HTTP auth")
self._client = connections.create_connection(
hosts=[{"host": self._host, "port": self._port}],
http_auth=http_auth,
use_ssl=self._use_ssl,
verify_certs=True,
connection_class=RequestsHttpConnection,
timeout=ELASTICSEARCH_DEFAULT_CONNECTION_TIMEOUT,
)
# Create a second connection with a timeout of 60s vs 10s.
# For some reason the PUT template API can take anywhere between
# 10s and 30s on the test cluster.
# This only needs to be done once to initialize the index template
connections.create_connection(
alias=ELASTICSEARCH_TEMPLATE_CONNECTION_ALIAS,
hosts=[{"host": self._host, "port": self._port}],
http_auth=http_auth,
use_ssl=self._use_ssl,
verify_certs=True,
connection_class=RequestsHttpConnection,
timeout=ELASTICSEARCH_TEMPLATE_CONNECTION_TIMEOUT,
)
try:
force_template_update = ELASTICSEARCH_FORCE_INDEX_TEMPLATE_UPDATE.lower() == "true"
self._client.indices.get_template(self._index_prefix)
LogEntry.init(
self._index_prefix,
self._index_settings,
skip_template_init=not force_template_update,
)
except NotFoundError:
LogEntry.init(self._index_prefix, self._index_settings, skip_template_init=False)
finally:
try:
connections.remove_connection(ELASTICSEARCH_TEMPLATE_CONNECTION_ALIAS)
except KeyError as ke:
logger.exception(
"Elasticsearch connection not found to remove %s: %s",
ELASTICSEARCH_TEMPLATE_CONNECTION_ALIAS,
ke,
)
self._initialized = True
示例12: get_conn
# 需要导入模块: from elasticsearch_dsl.connections import connections [as 别名]
# 或者: from elasticsearch_dsl.connections.connections import create_connection [as 别名]
def get_conn(*, verify=True, verify_indices=None):
"""
Lazily create the connection.
Args:
verify (bool): If true, check the presence of indices and mappings
verify_indices (list of str): If set, check the presence of these indices. Else use the defaults.
Returns:
elasticsearch.client.Elasticsearch: An Elasticsearch client
"""
# pylint: disable=global-statement
global _CONN
global _CONN_VERIFIED
do_verify = False
if _CONN is None:
http_auth = settings.ELASTICSEARCH_HTTP_AUTH
use_ssl = http_auth is not None
_CONN = connections.create_connection(
hosts=[settings.ELASTICSEARCH_URL],
http_auth=http_auth,
use_ssl=use_ssl,
# make sure we verify SSL certificates (off by default)
verify_certs=use_ssl
)
# Verify connection on first connect if verify=True.
do_verify = verify
if verify and not _CONN_VERIFIED:
# If we have a connection but haven't verified before, do it now.
do_verify = True
if not do_verify:
if not verify:
# We only skip verification if we're reindexing or
# deleting the index. Make sure we verify next time we connect.
_CONN_VERIFIED = False
return _CONN
# Make sure everything exists.
if verify_indices is None:
verify_indices = set()
for index_type in ALL_INDEX_TYPES:
verify_indices = verify_indices.union(
get_aliases(index_type)
)
for verify_index in verify_indices:
if not _CONN.indices.exists(verify_index):
raise ReindexException("Unable to find index {index_name}".format(
index_name=verify_index
))
_CONN_VERIFIED = True
return _CONN