diff --git a/amqpy/connection.py b/amqpy/connection.py index a27ca75..cb88e41 100644 --- a/amqpy/connection.py +++ b/amqpy/connection.py @@ -2,6 +2,8 @@ """ from __future__ import absolute_import, division, print_function +from future.moves.urllib.parse import urlparse, unquote + __metaclass__ = type import logging import socket @@ -55,7 +57,13 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, If you are using SSL, make sure the correct port number is specified (usually 5671), as the default of 5672 is for non-SSL connections. - :param str host: host + You can define an AMQP connection string as the host, this will be used to set + the `host`, `port`, `userid`, `password` and `virtual_host`. The connection string follows + this format: + + `amqp://[userid:password@]host[:port][/virtual_host]` + + :param str host: host or amqp connection string :param int port: port :param ssl: dict of SSL options passed to :func:`ssl.wrap_socket()`, None to disable SSL :param float connect_timeout: connect timeout @@ -111,6 +119,15 @@ def __init__(self, host='localhost', port=5672, ssl=None, connect_timeout=None, self._heartbeat_final = 0 # final heartbeat interval after negotiation self._heartbeat_server = None + # detect amqp connection string + if host.startswith('amqp://'): + parts = urlparse("http://" + host[7:]) + host = unquote(parts.hostname or '') or None + port = parts.port or 5672 + userid = unquote(parts.username or '') or 'guest' + password = unquote(parts.password or '') or 'guest' + virtual_host = unquote(parts.path[1:] or '/') + # save connection parameters self._host = host self._port = port diff --git a/setup.py b/setup.py index b7d8f8a..d6e83e9 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,10 @@ def long_description(): packages=find_packages(exclude=['ez_setup', 'tests', 'tests.*']), package_data=package_data, setup_requires=['six>=1.0'], - install_requires=['six>=1.0'], + install_requires=[ + 'six>=1.0', + 'future>=0.16.0', + ], tests_require=['pytest>=2.6'], classifiers=classifiers, keywords=keywords