Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def connect(self):
print "connecting"
try:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=self.config.test_rabbitmq.rabbitmq_host,
port=self.config.test_rabbitmq.rabbitmq_port,
virtual_host=self.config.test_rabbitmq.rabbitmq_vhost,
credentials=pika.credentials.PlainCredentials(
self.config.test_rabbitmq.rabbitmq_user,
self.config.test_rabbitmq.rabbitmq_password)))
except:
print "Failed to connect"
raise
self.connection = connection
if(self.exchange is None):
print 'Must pass in exchange!'
sys.exit(1)
if(self.queue is None):
print 'Must pass in queue!'
sys.exit(1)
if(self.topic is None):
print 'Must pass in topic!'
sys.exit(1)
params = pika.connection.ConnectionParameters(
host = self.host,
port = self.port,
virtual_host = self.virtual_host,
credentials = pika.credentials.PlainCredentials( username = self.username,
password = self.password))
self.connection = pika.BlockingConnection( parameters = params )
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.queue,
exclusive=True,
auto_delete=True
)
self.channel.exchange_declare( exchange = self.exchange,
exchange_type = self.exchange_type )
"""
def set_if_not_none(kwargs, key, val):
if val is not None:
kwargs[key] = val
broker = transporter_args["amqp_broker"]
port = transporter_args["amqp_port"]
vhost = transporter_args["amqp_vhost"]
queue_name = transporter_args["amqp_queue"]
user_name = transporter_args["amqp_user"]
password = transporter_args["amqp_password"]
logger.debug("Called stub method: call().")
self._queue_name = queue_name
credentials = pika.credentials.PlainCredentials(user_name, password)
conn_args = {}
set_if_not_none(conn_args, "host", broker)
set_if_not_none(conn_args, "port", port)
set_if_not_none(conn_args, "virtual_host", vhost)
set_if_not_none(conn_args, "credentials", credentials)
set_if_not_none(conn_args, "frame_max", MAX_FRAME_SIZE)
self.__setup_ssl(conn_args, transporter_args)
param = pika.connection.ConnectionParameters(**conn_args)
self.__connection = \
pika.adapters.blocking_connection.BlockingConnection(param)
self._channel = self.__connection.channel()
self._channel.queue_declare(queue=queue_name)
def connect():
curr_node = get_node_index(sys.argv[1])
while True:
try:
credentials = pika.credentials.PlainCredentials('jack', 'jack')
connection = pika.BlockingConnection(pika.ConnectionParameters(host=nodes[curr_node], port=5672, credentials=credentials))
channel = connection.channel()
print("Connected to " + nodes[curr_node])
return channel
except:
curr_node += 1
if curr_node > 2:
print("Could not connect. Trying again in 5 seconds")
time.sleep(5)
curr_node = 0
if self.config.get('tls'):
credentials = pika.credentials.ExternalCredentials()
config['rabbitmq']['credentials'] = credentials
context = ssl.create_default_context(
cafile=self.config['tls']['ca_cert']
)
context.load_cert_chain(
self.config['tls']['certfile'],
self.config['tls']['keyfile'],
)
ssl_options = pika.SSLOptions(context)
config['rabbitmq']['ssl_options'] = ssl_options
elif self.config.get('credentials'):
username = self.config['credentials'].get('username')
password = self.config['credentials'].get('password')
credentials = pika.credentials.PlainCredentials(
username=username,
password=password,
)
config['rabbitmq']['credentials'] = credentials
self.delivery_tag = None
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(**config['rabbitmq'])
)
self.channel = self.connection.channel()
# Which channel queue should we listen to?
if not self.config.get("channel_queue"):
raise IndexError(
"channel_queue must be configured for loopabull to work "
def __init__(self, config={}):
"""
stub init
"""
self.key = "FedmsgrabbitmqLooper"
self.config = config
super(FedmsgrabbitmqLooper, self).__init__(self)
# setup logging
self.logger = logging.getLogger("loopabull")
# Credentials to connect to the rabbitmq server
if self.config.get('tls'):
credentials = pika.credentials.ExternalCredentials()
config['rabbitmq']['credentials'] = credentials
context = ssl.create_default_context(
cafile=self.config['tls']['ca_cert']
)
context.load_cert_chain(
self.config['tls']['certfile'],
self.config['tls']['keyfile'],
)
ssl_options = pika.SSLOptions(context)
config['rabbitmq']['ssl_options'] = ssl_options
elif self.config.get('credentials'):
username = self.config['credentials'].get('username')
password = self.config['credentials'].get('password')
credentials = pika.credentials.PlainCredentials(
username=username,
password=password,
retry_delay=retry_delay,
heartbeat=heartbeat_interval,
ssl=True,
ssl_options=ssl_options,
credentials=pika.credentials.ExternalCredentials())
else:
# TODO: How is this working? PlainCredentials(rmq_user,
# rmq_user) ?? My understanding is that non ssl mode is going to
# be used only for testing - when using plain
# credentials all agents use same password.
conn_params = pika.ConnectionParameters(
host=self.rmq_config.hostname,
port=int(self.rmq_config.amqp_port),
virtual_host=self.rmq_config.virtual_host,
heartbeat=heartbeat_interval,
credentials=pika.credentials.PlainCredentials(
rmq_user, rmq_user))
except KeyError:
return None
return conn_params
def connect(self):
logging.debug("connecting to rabbit")
config = self.config.reprocesscrashlist
try:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=config.host,
port=config.port,
virtual_host=config.virtual_host,
credentials=pika.credentials.PlainCredentials(
config.rabbitmq_user,
config.rabbitmq_password))
)
except:
logging.error("Failed to connect")
raise
self.connection = connection
def __init__(self):
self._credentials = pika.credentials.PlainCredentials(
settings.RABBIT_MQ['username'], settings.RABBIT_MQ['password']
)
self._connection = None