Created
September 5, 2011 15:09
-
-
Save robcowie/1195200 to your computer and use it in GitHub Desktop.
pika credentials from uri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pika | |
amqp_re = re.compile(r'^amqp://'\ | |
'((?P<username>[^:]*)[:](?P<password>[^@]*)[@])?' \ | |
'((?P<host>[^/:]*)([:](?P<port>[^/]*))?)' \ | |
'(?P<virtual_host>/[^/]*)/?' \ | |
'([?](?P<query>[^/]*))?$') | |
def pika_credentials_from_url(amqp_url = None): | |
if not amqp_url: | |
amqp_url = 'amqp://127.0.0.1/' | |
params = amqp_re.match(amqp_url).groupdict() | |
credentials = pika.PlainCredentials( | |
params['username'] or 'guest', | |
params['password'] or 'guest', | |
) | |
conn_params = pika.ConnectionParameters( | |
params['host'] or '127.0.0.1', | |
port = int(params['port'] or '5672'), | |
virtual_host = params['virtual_host'] or '/', | |
credentials = credentials, | |
) | |
return conn_params |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment