Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
parser.add_option(
'--ssl', dest='ssl', action='store_true',
help='Enable SSL (default: not enabled)',
default=False,
)
options, args = parser.parse_args()
if not args:
parser.print_help()
sys.exit(1)
msg_body = ' '.join(args)
conn = amqp.Connection(options.host, userid=options.userid,
password=options.password, ssl=options.ssl)
ch = conn.channel()
ch.exchange_declare('myfan', 'fanout')
msg = amqp.Message(msg_body, content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'})
ch.basic_publish(msg, 'myfan')
ch.close()
conn.close()
)
parser.add_option(
'--ssl', dest='ssl', action='store_true',
help='Enable SSL (default: not enabled)',
default=False,
)
options, args = parser.parse_args()
if not args:
parser.print_help()
sys.exit(1)
msg_body = ' '.join(args)
conn = amqp.Connection(options.host, userid=options.userid,
password=options.password, ssl=options.ssl)
ch = conn.channel()
ch.exchange_declare('myfan', 'fanout')
msg = amqp.Message(msg_body, content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'})
ch.basic_publish(msg, 'myfan')
ch.close()
conn.close()
def handle_scan(states, amqp_config, barcode):
if states.get(barcode[:2], None):
values = dict( BARCODE=barcode[2:],
STATE=states.get(barcode[:2]),
API_KEY=amqp_config['api_key'] )
print values
data = xml % values
print data
conn = amqp.Connection(host=amqp_config['host']+":"+amqp_config['port'],
userid=amqp_config['userid'],
password=amqp_config['password'],
virtual_host=amqp_config['virtual_host'],)
chan = conn.channel()
msg = amqp.Message(data,
content_type='text/plain',
application_headers={'msg_type': 'sample_state_update'})
msg.properties["delivery_mode"] = 2
chan.basic_publish(msg,
exchange=amqp_config['exchange'],
routing_key=amqp_config['routing_key'])
chan.close()
conn.close()
def connection(self) -> Iterator[amqp.Connection]:
"""Returns a new connection as a context manager."""
TCP_USER_TIMEOUT = 18 # constant is available on Python 3.6+.
socket_settings = {TCP_USER_TIMEOUT: self.config.TCP_USER_TIMEOUT}
if sys.platform.startswith('darwin'):
del socket_settings[TCP_USER_TIMEOUT]
conn = amqp.Connection(
host="%s:%s" % (self.config.RABBIT_HOST, self.config.RABBIT_PORT),
userid=self.config.RABBIT_USER,
password=self.config.RABBIT_PASSWORD,
virtual_host=self.config.RABBIT_VIRTUAL_HOST,
connect_timeout=self.config.RABBIT_CONNECT_TIMEOUT,
read_timeout=self.config.RABBIT_READ_TIMEOUT,
write_timeout=self.config.RABBIT_WRITE_TIMEOUT,
socket_settings=socket_settings,
heartbeat=self.config.RABBIT_HEARTBEAT,
)
conn.connect()
logger.info('Connected to RabbitMQ')
with _safe_close(conn):
yield conn
def handle_scan(states, amqp_config, barcode):
if states.get(barcode[:2], None):
values = dict( BARCODE=barcode[2:],
STATE=states.get(barcode[:2]),
API_KEY=amqp_config['api_key'] )
print values
data = xml % values
print data
conn = amqp.Connection(host=amqp_config['host']+":"+amqp_config['port'],
userid=amqp_config['userid'],
password=amqp_config['password'],
virtual_host=amqp_config['virtual_host'],)
chan = conn.channel()
msg = amqp.Message(data,
content_type='text/plain',
application_headers={'msg_type': 'sample_state_update'})
msg.properties["delivery_mode"] = 2
chan.basic_publish(msg,
exchange=amqp_config['exchange'],
routing_key=amqp_config['routing_key'])
chan.close()
conn.close()
def ping_rabbitmq(address, port, usr, pwd, vhost):
try:
host = '{address}:{port}'.format(address=address, port=port)
amqp.Connection(host=host,
userid=usr,
password=pwd,
virtual_host=vhost)
msg = "rabbitmq vhost {0} on {1} is up".format(vhost,
address)
return [(status_ok, msg)]
except:
msg = 'rabbitmq vhost {0} on {1} is down'.format(vhost,
address)
return [(status_ko, msg)]
def __init__(self, *args, **kwargs):
self._rx = 0
self._messages = kwargs.pop("messages")
self._queues = 0
self._connection = amqp.Connection()
self._channel = self._connection.channel()
threading.Thread.__init__(self, *args, **kwargs)
def send_result_to_hub(json_data):
_log('Send JSON data to Hub: ' + json_data, level=logging.DEBUG)
o = urlparse(task_config.broker_url)
connection_params = {
'host': o.hostname,
'port': o.port,
'userid': o.username,
'password': o.password,
'virtual_host': o.path.replace('/', '')
}
with amqp.Connection(**connection_params) as c:
channel = c.channel()
channel.queue_declare(task_config.task_result_queue, durable=True, auto_delete=False)
channel.basic_publish(
amqp.Message(json_data, delivery_mode=PERSISTENT_DELIVERY_MODE),
routing_key=task_config.task_result_queue
)
priority=priority,
content_type=content_type,
content_encoding=content_encoding,
application_headers=headers,
**properties or {}
)
def prepare_queue_arguments(self, arguments, **kwargs):
return to_rabbitmq_queue_arguments(arguments, **kwargs)
def message_to_python(self, raw_message):
"""Convert encoded message body back to a Python value."""
return self.Message(raw_message, channel=self)
class Connection(amqp.Connection):
"""AMQP Connection."""
Channel = Channel
class Transport(base.Transport):
"""AMQP Transport."""
Connection = Connection
default_port = DEFAULT_PORT
default_ssl_port = DEFAULT_SSL_PORT
# it's very annoying that pyamqp sometimes raises AttributeError
# if the connection is lost, but nothing we can do about that here.
connection_errors = amqp.Connection.connection_errors