def __init__(self, host="127.0.0.1", port=5672, username="", password="", **connection_options):
"""
Event transport via RabbitMQ server.
:param host: ipv4 or hostname
:param port: the port where the server listens
:param username: username used for authentication
:param password: password used for authentication
:param connection_options: extra arguments that will be used in
:py:class:`pika.BlockingConnection` initialization.
"""
if not pika:
raise RuntimeError("RabbitMqEventTransport requires 'pika' to run")
super(RabbitMqEventTransport, self).__init__()
self._handlers = {}
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=host, port=port,
credentials=pika.PlainCredentials(username=username, password=password),
**connection_options
)
)
self.channel = self.connection.channel()
python类PlainCredentials()的实例源码
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)
def receive(self,callback, username, pwd, ip, port):
'''
# ????????????????????????
:param callback: ????
:param username: ??RabbitMQ???????
:param pwd: ??
:param ip: ip
:param port: ??
:return:
'''
user_pwd = pika.PlainCredentials(username, pwd)
s_conn = pika.BlockingConnection(pika.ConnectionParameters(ip, port, '/', credentials=user_pwd))
channel = s_conn.channel()
channel.queue_declare(queue='city_task_queue', durable=True)
channel.basic_qos(prefetch_count=1)
print '???????????'
channel.basic_consume(callback,
queue='city_task_queue',
)
channel.start_consuming()
def connect(self):
if self.connecting:
print('PikaClient: Already connecting to RabbitMQ')
return
print('PikaClient: Connecting to RabbitMQ on localhost:5672, Object: %s' % (self,))
self.connecting = True
# credentials = pika.PlainCredentials('guest', 'guest')
# param = pika.ConnectionParameters(host='localhost',
# port=5672,
# virtual_host="/"
# credentials=credentials
# )
param = pika.ConnectionParameters("localhost")
self.connection = TornadoConnection(param,
on_open_callback=self.on_connected)
#Currently this will close tornado ioloop.
#self.connection.add_on_close_callback(self.on_closed)
def connect(self):
"""
Create blocking connection in RMQ
:return: pika.BlockingConnection
"""
return BlockingConnection(
parameters=ConnectionParameters(
host=self.host,
port=self.port,
virtual_host=self.vhost,
credentials=PlainCredentials(
username=self.user,
password=self.passwd,
)
)
)
def __init__(self, cb_server_address, rmq_username, rmq_password, routing_key):
self.q = Queue.Queue()
self.go = True
# in case the cb url is passed in (which is often required for API stuff),
# try to parse out the IP/DNS information.
# This could be cleaner and better.
cb_server_address = cb_server_address.lower()
if cb_server_address.startswith("https://"):
cb_server_address = cb_server_address[8:]
elif cb_server_address.startswith("http://"):
cb_server_address = cb_server_address[7:]
cb_server_address = cb_server_address.split('/')[0]
# Set the connection parameters to connect to rabbit-server1 on port 5672
# on the / virtual host using the username "guest" and password "guest"
credentials = pika.PlainCredentials(rmq_username, rmq_password)
parameters = pika.ConnectionParameters(cb_server_address,
5004,
'/',
credentials)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
queue_name = self.__generate_queue_name()
# make sure you use auto_delete so the queue isn't left filling
# with events when this program exists.
self.channel.queue_declare(queue=queue_name, auto_delete=True)
self.channel.queue_bind(exchange='api.events', queue=queue_name, routing_key=routing_key)
self.channel.basic_consume(self.__on_message, queue=queue_name)
threading.Thread.__init__(self)
cluster_auto_start_daemon.py 文件源码
项目:sm-engine-ansible
作者: METASPACE2020
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def queue_empty(self):
try:
creds = pika.PlainCredentials(self.ansible_config['rabbitmq_user'],
self.ansible_config['rabbitmq_password'])
conn = pika.BlockingConnection(pika.ConnectionParameters(host=self.ansible_config['rabbitmq_host'],
credentials=creds))
ch = conn.channel()
m = ch.queue_declare(queue=self.qname, durable=True, arguments={'x-max-priority': 3})
self.logger.debug('Messages in the queue: {}'.format(m.method.message_count))
return m.method.message_count == 0
except Exception as e:
self.logger.warning(e, exc_info=True)
return True
def __init__(self, host, port, user, password, vhost, queue_name):
credentials = pika.PlainCredentials(user, password)
parameters = pika.ConnectionParameters(
host=host, port=port,
virtual_host=vhost, credentials=credentials
)
self.queue_name = queue_name
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
self.channel.confirm_delivery()
def getMqConnection(mqConf,vErrors,maxMsgTotal):
# try to connect via amqp
amqpLink = pika.BlockingConnection(
pika.ConnectionParameters(
mqConf["server"],
amqpPort,
mqConf.get("vhost",'/'),
pika.PlainCredentials(mqConf["user"], mqConf["pwd"])))
return amqpLink
def __init__(self, parent):
"""
This module will communicate via RPC
with RabbitMQ and ultimately with
our fuzzing server
"""
self.parent = parent
self.ae = parent.ae
self.cfg = parent.cfg
host = self.cfg.get('server_info', 'host')
credentials = pika.PlainCredentials(
self.cfg.get('server_info', 'user'),
self.cfg.get('server_info', 'pass'))
try:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(
host = host,
credentials = credentials,
retry_delay = 10,
connection_attempts = 5))
self.ae.m_ok("Connected to server (broker): %s" % host)
except Exception as e:
self.ae.m_fatal("Could not connect to server")
self.ae.m_fatal(e)
self.channel = self.connection.channel()
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)
def __init__(self, parent):
"""
This module will communicate via RPC
with RabbitMQ and ultimately with
our fuzzing server
"""
self.parent = parent
self.ae = parent.ae
self.cfg = parent.cfg
host = self.cfg.get('server_info', 'host')
credentials = pika.PlainCredentials(
self.cfg.get('server_info', 'user'),
self.cfg.get('server_info', 'pass'))
try:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(
host = host,
credentials = credentials,
retry_delay = 10,
connection_attempts = 5))
self.ae.m_ok("Connected to server (broker): %s" % host)
except Exception as e:
self.ae.m_fatal("Could not connect to server")
self.ae.m_fatal(e)
self.channel = self.connection.channel()
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)
def __init__(self):
print("Authenticating with rabbitmq server : %s" % HOSTURI)
credentials = pika.PlainCredentials('simplerpc', 'simplerpc')
connURI = pika.ConnectionParameters(host=HOSTURI,
credentials=credentials)
self.connection = pika.BlockingConnection(connURI)
self.channel = self.connection.channel()
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)
print("Authentication seems to be successful!")
def rabbit_connect(rabbit_user, rabbit_pass, rabbit_host, rabbit_port, rabbit_virtual_host, rabbit_heartbeat):
credentials = pika.PlainCredentials(rabbit_user, rabbit_pass)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbit_host, rabbit_port, rabbit_virtual_host,
credentials, heartbeat_interval=rabbit_heartbeat))
return connection
# close connection to rabbit function
def rabbit_connect(rabbit_user, rabbit_pass, rabbit_host, rabbit_port, rabbit_virtual_host, rabbit_heartbeat):
credentials = pika.PlainCredentials(rabbit_user, rabbit_pass)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbit_host, rabbit_port, rabbit_virtual_host,
credentials, heartbeat_interval=rabbit_heartbeat))
return connection
# close connection to rabbit function
def __init__(self, args):
"""Construct object and save forwarded arguments."""
verbose_to_log = {
0: logging.CRITICAL,
1: logging.ERROR,
2: logging.WARN,
3: logging.INFO,
4: logging.DEBUG
}
logging_level = logging.DEBUG if args.verbose > 4 else verbose_to_log[args.verbose]
log.setLevel(logging_level)
log.debug("args: %s" % args)
self.args = args
config = ConfigParser()
config_entries = config.read(self.args.config_path)
self.whitelist = [i.strip() for i in args.whitelist.split(',')]
if config_entries:
self.whitelist += [i.strip() for i in config.get(self.args.product, 'whitelist').split(',')]
else:
log.info("No configuration file '{}' for whitelist, only using optionally specified command line whitelist".format(self.args.config_path))
log.debug(CONFIG_USAGE)
# does not look so nice, can be improved. Removing empty string entries.
self.whitelist = [i for i in self.whitelist if i]
log.info("Whitelist content for %s: %s" % (self.args.product, self.whitelist))
self.release_info_path = os.path.join(self.args.dest, self.args.release_file)
self.browser = Browser(args, args.openqa_host)
if not config.has_section('notification'):
return
self.credentials = pika.PlainCredentials(config.get('notification', 'username', fallback='guest'),
config.get('notification', 'password', fallback='guest'))
self.notify_host = config.get('notification', 'host', fallback='kazhua.suse.de')
self.notify_connect()
def _connect(self):
"""Initial connection to the queue manager."""
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='dazzar_rabbitmq',
credentials=pika.PlainCredentials(
self.username,
self.password)))
self.channel = self.connection.channel()
self.channel.basic_qos(prefetch_count=1)
self.channel.queue_declare(queue='dazzar_jobs', durable=True)
def run(argv):
pub_ip = ""
l.info("JOB RUN : " + pformat(argv))
if len(argv) > 1:
pub_ip = argv[1]
if not pub_ip:
raise Exception("Rmq-sub needs a pub server to subscribe to, pub_ip"
" can not be empty pub_ip[%s]" % (pub_ip))
# Initalize HDaemonRepSrv
sub_rep_port = os.environ.get('PORT0')
hd = HDRmqsRepSrv(sub_rep_port)
hd.reset_stats()
hd.run()
l.info("RabbitMQ SUB client connecting to RabbitMQ PUB server at [%s]" % (pub_ip))
credentials = pika.PlainCredentials('hydra', 'hydra')
connection = pika.BlockingConnection(pika.ConnectionParameters(host=pub_ip, credentials=credentials))
channel = connection.channel()
channel.exchange_declare(exchange='pub', type='fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='pub', queue=queue_name)
l.info("RabbitMQ SUB client succesfully connected to RabbitMQ PUB server at [%s]" % (pub_ip))
hd.msg_cnt = 0
channel.basic_consume(hd.callback, queue=queue_name, no_ack=True)
channel.start_consuming()
def connect(self):
"""
?rabbitmq ???????????????????????
:return:
"""
if self.connect_count <= 0 and self.connection is None or self.connection.is_closed:
credentials = pika.PlainCredentials(self.user, self.pwd)
parameters = pika.ConnectionParameters(self.host, self.port, '/', credentials)
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
self.connect_count += 1
def send(self, message_list, username, pwd, ip, port,queue_name):
'''
????????????
:param message_list ????
:param username ??RabbitMQ??????
:param pwd ??
:param ip ?????ip
:param port ??
:param queue_name ??????????
:return
'''
user_pwd = pika.PlainCredentials(username, pwd)
s_conn = pika.BlockingConnection(pika.ConnectionParameters(ip, port, '/', credentials=user_pwd)) # ????
channel = s_conn.channel() # ??????????
channel.queue_declare(queue=queue_name, durable=True) # ???????task_queue????????????????????????????
print '???????????:', len(message_list)
for g in range(len(message_list)):
message = message_list[g]
message = message.encode('utf-8')#??????????????????
channel.basic_publish(exchange='',
routing_key=queue_name, # ??????????worker
body=message, # ??????
properties=pika.BasicProperties(delivery_mode=2, ) # ?????????????????????2??????????
)
print g, ':', message
time.sleep(0.02) # ????
print g
def receive(self,username,pwd,ip,port):#????????????????????????
user_pwd = pika.PlainCredentials(username, pwd)
s_conn = pika.BlockingConnection(pika.ConnectionParameters(ip,port,'/', credentials=user_pwd))
channel = s_conn.channel()
channel.queue_declare(queue='city_task_queue', durable=True)
channel.basic_qos(prefetch_count=1)
print '???????????'
channel.basic_consume(self.callback,
queue='city_task_queue',
)
channel.start_consuming()
def rabbit_connect(rabbit_user, rabbit_pass, rabbit_host, rabbit_port, rabbit_virtual_host, rabbit_heartbeat):
credentials = pika.PlainCredentials(rabbit_user, rabbit_pass)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbit_host, rabbit_port, rabbit_virtual_host,
credentials, heartbeat_interval=rabbit_heartbeat))
return connection
# close connection to rabbit function
def rabbit_connect(rabbit_user, rabbit_pass, rabbit_host, rabbit_port, rabbit_virtual_host, rabbit_heartbeat):
credentials = pika.PlainCredentials(rabbit_user, rabbit_pass)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbit_host, rabbit_port, rabbit_virtual_host,
credentials, heartbeat_interval=rabbit_heartbeat))
return connection
# close connection to rabbit function
def __init__(self, addr):
threading.Thread.__init__(self)
ip, port = addr
self.url = ip + '/' + str(port)
creden = pika.PlainCredentials(RABBITU, RABBITP)
params = pika.ConnectionParameters(host=ip, credentials=creden)
self.connection = pika.BlockingConnection(params)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.url)
# self.channel.basic_qos(prefetch_count=1)
def __init__(self, url):
aurl = urlparse(url)
address = aurl.netloc.split(':')
ip, port = address[0], int(address[1])
self.url = ip + '/' + str(port)
creden = pika.PlainCredentials(RABBITU, RABBITP)
params = pika.ConnectionParameters(host=ip, credentials=creden)
self.connection = pika.BlockingConnection(params)
self.channel = self.connection.channel()
def connect(self):
if self.connecting:
return
self.connecting = True
cred = pika.PlainCredentials('guest', 'guest')
param = pika.ConnectionParameters(
host='localhost',
port=5672,
virtual_host='/',
credentials=cred
)
self.connection = TornadoConnection(param,
on_open_callback=self.on_connected)
def connection(server, user, password):
credentials = pika.PlainCredentials(user, password)
try:
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=server,
credentials=credentials,
)
)
return connection
except:
message = "Failed rabbit connection to server {}".format(server)
log.exception(message)
raise RabbitConnectionFailed(message)
def _connect(self):
logging.info("Connecting to AMQP broker")
self.connection = pika.BlockingConnection(pika.ConnectionParameters(
host=os.environ['AMQP_HOST'],
credentials=pika.PlainCredentials(os.environ['AMQP_USER'],
os.environ['AMQP_PASSWORD'])))
self.channel = self.connection.channel()
self.channel.queue_declare(queue='build_queue', durable=True)
self.channel.queue_declare(queue='run_queue', durable=True)
self.channel.basic_qos(prefetch_count=1)
connection.py 文件源码
项目:Distributed-Multi-User-Scrapy-System-with-a-Web-UI
作者: aaldaber
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def from_settings(settings, spider_name):
connection_type = settings.get('RABBITMQ_CONNECTION_TYPE',
RABBITMQ_CONNECTION_TYPE)
queue_name = "%s:requests" % spider_name
connection_host = settings.get('RABBITMQ_HOST')
connection_port = settings.get('RABBITMQ_PORT')
connection_username = settings.get('RABBITMQ_USERNAME')
connection_pass = settings.get('RABBITMQ_PASSWORD')
connection_attempts = 5
retry_delay = 3
credentials = pika.PlainCredentials(connection_username, connection_pass)
connection = {
'blocking': pika.BlockingConnection,
'libev': pika.LibevConnection,
'select': pika.SelectConnection,
'tornado': pika.TornadoConnection,
'twisted': pika.TwistedConnection
}[connection_type](pika.ConnectionParameters(host=connection_host,
port=connection_port, virtual_host='/',
credentials=credentials,
connection_attempts=connection_attempts,
retry_delay=retry_delay))
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
return channel
def __init__(self):
"""Create a new instance of the consumer class, passing in the AMQP
URL used to connect to RabbitMQ.
"""
pi('__init__')
self._connection = None
self._connected = False
self._connecting = False
self._channel = None
self._closing = False
self._closed = False
self._consumer_tag = None
self._deliveries = []
self._acked = 0
self._nacked = 0
self._message_number = 0
self._credentials = pika.PlainCredentials('guest', 'guest')
self._parameters = pika.ConnectionParameters(host='localhost',
port=PORT,
virtual_host='/',
credentials=self._credentials)
self._queue = 'queue-' + str(uuid.uuid4())
self.websocket = None
self._status = 0
self._person = None
self._clientid = None
self._participants = 0
pp(self, '__INIT__')
pr('__init__')
def __init__(self, credentials=None, params=None, queue=None):
"""Create a new instance of the consumer class, passing in the AMQP
URL used to connect to RabbitMQ.
:param credentials: credentials to connect to rabbitmq broker server
:type credentials: pika.credentials.PlainCredentials
:param params: connection paramaters used to connect with rabbitmq broker server
:type params: pika.connection.ConnectionParameters
:param queue: queue to be created after a channel is established which will be bound to an exchange
:type queue: string - random long base64 url safe encoded string
"""
self._connection = None
self._connected = False
self._connecting = False
self._channel = None
self._closing = False
self._closed = False
self._consumer_tag = None
self._deliveries = []
self._acked = 0
self._nacked = 0
self._message_number = 0
self._credentials = credentials if credentials else pika.PlainCredentials('guest', 'guest')
self._parameters = params if params else pika.ConnectionParameters(host='localhost',
port=PORT,
virtual_host='/',
credentials=self._credentials)
self._queue = queue if queue else 'queue-' + str(uuid.uuid4())
self.websocket = None
self._status = 0
self._person = None
self._clientid = None
self._participants = 0