/usr/lib/python3/dist-packages/consul/aio.py is in python3-consul 0.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | from __future__ import absolute_import
import sys
import asyncio
import warnings
import aiohttp
from consul import base
__all__ = ['Consul']
PY_341 = sys.version_info >= (3, 4, 1)
class HTTPClient(base.HTTPClient):
"""Asyncio adapter for python consul using aiohttp library"""
def __init__(self, *args, loop=None, **kwargs):
super(HTTPClient, self).__init__(*args, **kwargs)
self._loop = loop or asyncio.get_event_loop()
connector = aiohttp.TCPConnector(loop=self._loop,
verify_ssl=self.verify)
self._session = aiohttp.ClientSession(connector=connector)
@asyncio.coroutine
def _request(self, callback, method, uri, data=None):
resp = yield from self._session.request(method, uri, data=data)
body = yield from resp.text(encoding='utf-8')
if resp.status == 599:
raise base.Timeout
r = base.Response(resp.status, resp.headers, body)
return callback(r)
# python prior 3.4.1 does not play nice with __del__ method
if PY_341: # pragma: no branch
def __del__(self):
if not self._session.closed:
warnings.warn("Unclosed connector in aio.Consul.HTTPClient",
ResourceWarning)
self.close()
def get(self, callback, path, params=None):
uri = self.uri(path, params)
return self._request(callback, 'GET', uri)
def put(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return self._request(callback, 'PUT', uri, data=data)
def delete(self, callback, path, params=None):
uri = self.uri(path, params)
return self._request(callback, 'DELETE', uri)
def post(self, callback, path, params=None, data=''):
uri = self.uri(path, params)
return self._request(callback, 'POST', uri, data=data)
def close(self):
self._session.close()
class Consul(base.Consul):
def __init__(self, *args, loop=None, **kwargs):
self._loop = loop or asyncio.get_event_loop()
super().__init__(*args, **kwargs)
def connect(self, host, port, scheme, verify=True, cert=None):
return HTTPClient(host, port, scheme, loop=self._loop,
verify=verify, cert=None)
def close(self):
"""Close all opened http connections"""
self.http.close()
|