-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
13 changed files
with
1,656 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
language: python | ||
dist: bionic | ||
dist: focal | ||
python: | ||
- 3.5 | ||
- 3.6 | ||
- 3.7 | ||
- 3.8 | ||
- 3.9 | ||
services: | ||
- rabbitmq | ||
install: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM python:3.5 | ||
FROM python:3.9 | ||
|
||
WORKDIR /usr/src/app | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import asyncio | ||
import socket | ||
from urllib.parse import urlparse | ||
|
||
from .exceptions import * # pylint: disable=wildcard-import | ||
from .protocol import AmqpProtocol | ||
|
||
from .version import __version__ | ||
from .version import __packagename__ | ||
|
||
|
||
async def connect(host='localhost', port=None, login='guest', password='guest', | ||
virtualhost='/', ssl=None, login_method='PLAIN', insist=False, | ||
protocol_factory=AmqpProtocol, *, loop=None, **kwargs): | ||
"""Convenient method to connect to an AMQP broker | ||
@host: the host to connect to | ||
@port: broker port | ||
@login: login | ||
@password: password | ||
@virtualhost: AMQP virtualhost to use for this connection | ||
@ssl: SSL context used for secure connections, omit for no SSL | ||
- see https://docs.python.org/3/library/ssl.html | ||
@login_method: AMQP auth method | ||
@insist: Insist on connecting to a server | ||
@protocol_factory: | ||
Factory to use, if you need to subclass AmqpProtocol | ||
@loop: Set the event loop to use | ||
@kwargs: Arguments to be given to the protocol_factory instance | ||
Returns: a tuple (transport, protocol) of an AmqpProtocol instance | ||
""" | ||
if loop is None: | ||
loop = asyncio.get_event_loop() | ||
factory = lambda: protocol_factory(loop=loop, **kwargs) | ||
|
||
create_connection_kwargs = {} | ||
|
||
if ssl is not None: | ||
create_connection_kwargs['ssl'] = ssl | ||
|
||
if port is None: | ||
if ssl: | ||
port = 5671 | ||
else: | ||
port = 5672 | ||
|
||
transport, protocol = await loop.create_connection( | ||
factory, host, port, **create_connection_kwargs | ||
) | ||
|
||
# these 2 flags *may* show up in sock.type. They are only available on linux | ||
# see https://bugs.python.org/issue21327 | ||
nonblock = getattr(socket, 'SOCK_NONBLOCK', 0) | ||
cloexec = getattr(socket, 'SOCK_CLOEXEC', 0) | ||
sock = transport.get_extra_info('socket') | ||
if sock is not None and (sock.type & ~nonblock & ~cloexec) == socket.SOCK_STREAM: | ||
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) | ||
|
||
try: | ||
await protocol.start_connection(host, port, login, password, virtualhost, ssl=ssl, login_method=login_method, | ||
insist=insist) | ||
except Exception: | ||
await protocol.wait_closed() | ||
raise | ||
|
||
return transport, protocol | ||
|
||
|
||
async def from_url( | ||
url, login_method='PLAIN', insist=False, protocol_factory=AmqpProtocol, **kwargs): | ||
""" Connect to the AMQP using a single url parameter and return the client. | ||
For instance: | ||
amqp://user:password@hostname:port/vhost | ||
@insist: Insist on connecting to a server | ||
@protocol_factory: | ||
Factory to use, if you need to subclass AmqpProtocol | ||
@loop: optionally set the event loop to use. | ||
@kwargs: Arguments to be given to the protocol_factory instance | ||
Returns: a tuple (transport, protocol) of an AmqpProtocol instance | ||
""" | ||
url = urlparse(url) | ||
|
||
if url.scheme not in ('amqp', 'amqps'): | ||
raise ValueError('Invalid protocol %s, valid protocols are amqp or amqps' % url.scheme) | ||
|
||
transport, protocol = await connect( | ||
host=url.hostname or 'localhost', | ||
port=url.port, | ||
login=url.username or 'guest', | ||
password=url.password or 'guest', | ||
virtualhost=(url.path[1:] if len(url.path) > 1 else '/'), | ||
login_method=login_method, | ||
insist=insist, | ||
protocol_factory=protocol_factory, | ||
**kwargs) | ||
return transport, protocol |
Oops, something went wrong.