本文整理汇总了Python中pika.ConnectionParameters方法的典型用法代码示例。如果您正苦于以下问题:Python pika.ConnectionParameters方法的具体用法?Python pika.ConnectionParameters怎么用?Python pika.ConnectionParameters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika
的用法示例。
在下文中一共展示了pika.ConnectionParameters方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def connect(self):
"""This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
:rtype: pika.SelectConnection
"""
self.logger.info('Connecting to %s', self._prefix)
account = 'opensuse'
server = 'rabbit.opensuse.org'
if self._prefix == 'suse':
account = 'suse'
server = 'rabbit.suse.de'
credentials = pika.PlainCredentials(account, account)
context = ssl.create_default_context()
ssl_options = pika.SSLOptions(context, server)
parameters = pika.ConnectionParameters(server, 5671, '/', credentials, ssl_options=ssl_options, socket_timeout=10)
return pika.SelectConnection(parameters,
on_open_callback=self.on_connection_open)
示例2: get_connection_amqp
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def get_connection_amqp():
try:
port = int(config.get('ckan.harvest.mq.port', PORT))
except ValueError:
port = PORT
userid = config.get('ckan.harvest.mq.user_id', USERID)
password = config.get('ckan.harvest.mq.password', PASSWORD)
hostname = config.get('ckan.harvest.mq.hostname', HOSTNAME)
virtual_host = config.get('ckan.harvest.mq.virtual_host', VIRTUAL_HOST)
credentials = pika.PlainCredentials(userid, password)
parameters = pika.ConnectionParameters(host=hostname,
port=port,
virtual_host=virtual_host,
credentials=credentials,
frame_max=10000)
log.debug("pika connection using %s" % parameters.__dict__)
return pika.BlockingConnection(parameters)
示例3: get_connection_params_dict
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def get_connection_params_dict(cls):
"""
Get the AMQP connection parameters (as a dict)
using n6lib.amqp_helpers.get_amqp_connection_params_dict()
and the `SOCKET_TIMEOUT` class constant.
Returns:
A dict that can be used as **kwargs for pika.ConnectionParameters.
"""
conn_params_dict = get_amqp_connection_params_dict(cls.rabbitmq_config_section)
conn_params_dict.update(
socket_timeout=cls.SOCKET_TIMEOUT,
)
return conn_params_dict
#
# Regular instance methods
# Start/stop-related stuff:
示例4: connect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def connect(self):
"""
From pika docs:
This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
Returns:
pika.SelectConnection
"""
LOGGER.info('Connecting to %s', ascii_str(self._conn_params_dict['host']))
if self.input_queue is not None or self.output_queue is not None:
self._amqp_setup_timeout_callback_manager.deactivate()
self._amqp_setup_timeout_callback_manager.activate()
return pika.SelectConnection(
pika.ConnectionParameters(**self._conn_params_dict),
self.on_connection_open,
self.on_connection_error_open,
stop_ioloop_on_close=True,
)
示例5: connect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def connect(self):
try:
self.connected_node = self.broker_manager.get_current_node(self.consumer_id)
ip = self.broker_manager.get_node_ip(self.connected_node)
console_out(f"Connecting to {self.connected_node}", self.get_actor())
credentials = pika.PlainCredentials('jack', 'jack')
parameters = pika.ConnectionParameters(ip,
self.broker_manager.get_consumer_port(self.connected_node, self.consumer_id),
'/',
credentials)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
if self.prefetch > 0:
self.channel.basic_qos(prefetch_count=self.prefetch)
return True
except Exception as e:
console_out_exception("Failed trying to connect.", e, self.get_actor())
return False
示例6: __init__
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def __init__(self, jobs, responses, host='localhost', port=5672,
user='guest', password='guest', rabbit_queue='rpc_queue'):
# Set connection and channel
self.credentials = pika.PlainCredentials(user, password)
self.parameters = pika.ConnectionParameters(host, port, '/', self.credentials)
self.connection = pika.BlockingConnection(self.parameters)
self.channel = self.connection.channel()
# Set queue for jobs and callback queue for responses
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response, no_ack=True, queue=self.callback_queue)
self.rabbit_queue = rabbit_queue
self.channel.queue_declare(queue=self.rabbit_queue)
self.response = None
self.id = None
# Local queues shared between threads
self.jobs = jobs
self.responses = responses
# Report to the RabbitMQ server
heartbeat_thread = threading.Thread(target=self.heartbeat)
heartbeat_thread.daemon = True
heartbeat_thread.start()
示例7: sendData
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def sendData(self, server, queue, ci, data):
self.logger.debug('Sending [{}] to [{}] Start'.format(ci, server))
## conn = pika.BlockingConnection(
## pika.ConnectionParameters(server)
## )
with pika.BlockingConnection(pika.ConnectionParameters(host = server,
port = self.MQ_PORT)) as conn:
for line in data:
chan = conn.channel()
chan.queue_declare(queue = queue, durable = True)
chan.basic_publish(exchange = '', routing_key = queue, body = line,
properties = pika.BasicProperties(delivery_mode = 2, ))
self.logger.debug('Sending [{}] to [{}] Done'.format(ci, server))
return(True)
## run func
示例8: scmb_connect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def scmb_connect(self):
scmb_server = self.ov_ip
# Setup our ssl options
ssl_options = ({'ca_certs': _oneview_ca_path(self.ov_ip),
'certfile': _scmb_cert_path(self.ov_ip),
'keyfile': _scmb_key_path(self.ov_ip),
'cert_reqs': ssl.CERT_REQUIRED,
'server_side': False})
scmb_connection = pika.BlockingConnection(
pika.ConnectionParameters(
scmb_server,
SCMB_PORT,
credentials=ExternalCredentials(),
socket_timeout=SCMB_SOCKET_TIMEOUT,
ssl=True,
ssl_options=ssl_options))
return scmb_connection
示例9: __init__
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def __init__(self,
amqp_user,
amqp_pass,
amqp_host,
amqp_vhost,
ssl_enabled,
ssl_cert_path):
self.connection = None
self.channel = None
self._is_closed = False
credentials = pika.credentials.PlainCredentials(
username=amqp_user,
password=amqp_pass)
ssl_options = utils.internal.get_broker_ssl_options(ssl_enabled,
ssl_cert_path)
self._connection_parameters = pika.ConnectionParameters(
host=amqp_host,
port=BROKER_PORT_SSL if ssl_enabled else BROKER_PORT_NO_SSL,
virtual_host=amqp_vhost,
socket_timeout=self.SOCKET_TIMEOUT,
connection_attempts=self.CONNECTION_ATTEMPTS,
credentials=credentials,
ssl=bool(ssl_enabled),
ssl_options=ssl_options)
self._connect()
示例10: main
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def main(queue_name, host): # pragma: no cover
"""Creates the connection to RabbitMQ as a consumer and binds to the queue
waiting for messages
"""
counter = 0
while True:
counter += 1
try:
params = pika.ConnectionParameters(host=host, port=5672)
connection = pika.BlockingConnection(params)
channel = connection.channel()
print('Connected to rabbit')
channel.queue_declare(queue=queue_name, durable=True)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(
queue=queue_name, on_message_callback=callback)
channel.start_consuming()
except Exception as e: # pragma: no cover
print(str(e))
print(
'Waiting for connection to rabbit...attempt: {0}'.format(counter))
time.sleep(1)
return
示例11: _connect_rabbit
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def _connect_rabbit():
# Rabbit settings
exchange = 'topic-poseidon-internal'
exchange_type = 'topic'
# Starting rabbit connection
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='RABBIT_SERVER')
)
channel = connection.channel()
channel.exchange_declare(
exchange=exchange, exchange_type=exchange_type
)
return channel, exchange, connection
示例12: connect
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def connect(self):
"""This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
:rtype: pika.SelectConnection
"""
return pika.SelectConnection(
pika.ConnectionParameters(
host='localhost',
virtual_host=self.VIRTUAL_HOST,
credentials=self.CREDENTIALS
),
self.on_connection_open,
stop_ioloop_on_close=False
)
示例13: create_connection_parameters
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def create_connection_parameters(self):
tolog("MQ ARGO: create connection parameters, server = " + self.host + " port = " + str(self.port))
# need to set credentials to login to the message server
#self.credentials = pika.PlainCredentials(self.username,self.password)
self.credentials = pika.credentials.ExternalCredentials()
ssl_options_dict = {
"certfile": self.ssl_cert,
"keyfile": self.ssl_key,
"ca_certs": self.ssl_ca_certs,
"cert_reqs": ssl.CERT_REQUIRED,
}
#logger.debug(str(ssl_options_dict))
# setup our connection parameters
self.parameters = pika.ConnectionParameters(
host = self.host,
port = self.port,
virtual_host = self.virtual_host,
credentials = self.credentials,
socket_timeout = self.socket_timeout,
ssl = True,
ssl_options = ssl_options_dict,
)
示例14: rabbit_conn
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def rabbit_conn(self):
"""Make connection to rabbit to send events"""
# check if a rabbit host was specified
if not self.host:
print('Not connecting to any RabbitMQ, host is None.') # pylint: disable=print-statement
return False
# create connection to rabbit
params = pika.ConnectionParameters(host=self.host,
port=self.port,
heartbeat=600,
blocked_connection_timeout=300)
try:
self.channel = pika.BlockingConnection(params).channel()
self.channel.exchange_declare(exchange=self.exchange,
exchange_type=self.exchange_type)
except (pika.exceptions.AMQPError, socket.gaierror, OSError) as err:
print("Unable to connect to RabbitMQ at %s:%s because: %s" %
(self.host, self.port, err)) # pylint: disable=print-statement
return False
print("Connected to RabbitMQ at %s:%s" % (self.host, self.port)) # pylint: disable=print-statement
return True
示例15: main
# 需要导入模块: import pika [as 别名]
# 或者: from pika import ConnectionParameters [as 别名]
def main():
"""Creates the connection to RabbitMQ as a consumer and binds to the queue
waiting for messages
"""
params = pika.ConnectionParameters(host="0.0.0.0", port=5672)
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.exchange_declare(exchange='topic_recs', exchange_type='topic')
result = channel.queue_declare()
queue_name = result.method.queue
binding_key = "FAUCET.Event"
channel.queue_bind(exchange='topic_recs',
queue=queue_name,
routing_key=binding_key)
return channel, queue_name