aiohttp.TCPConnector

Here are the examples of the python api aiohttp.TCPConnector taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

67 Examples 7

Page 1 Selected Page 2

Example 1

    def test_dont_close_explicit_connector(self):

        @asyncio.coroutine
        def go(url):
            connector = aiohttp.TCPConnector(loop=self.loop)

            r = yield from client.request('GET', url,
                                          connector=connector,
                                          loop=self.loop)
            yield from r.read()
            self.assertEqual(1, len(connector._conns))
            connector.close()

        with run_server(self.loop, router=Functional) as httpd:
            url = httpd.url('keepalive')
            self.loop.run_until_complete(go(url))

Example 2

Project: aiohttp
License: View license
Source File: test_connector.py
def test_both_use_dns_cache_only(loop):
    conn = aiohttp.TCPConnector(use_dns_cache=True,
                                loop=loop)
    assert conn.use_dns_cache
    with pytest.warns(DeprecationWarning):
        assert conn.resolve

Example 3

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_clear_resolved_hosts(loop):
    conn = aiohttp.TCPConnector(loop=loop)
    info = object()
    conn._cached_hosts[('localhost', 123)] = info
    conn._cached_hosts[('localhost', 124)] = info
    conn.clear_resolved_hosts('localhost', 123)
    assert conn.resolved_hosts == {('localhost', 124): info}
    conn.clear_resolved_hosts('localhost', 123)
    assert conn.resolved_hosts == {('localhost', 124): info}
    with pytest.warns(DeprecationWarning):
        conn.clear_resolved_hosts()
    assert conn.resolved_hosts == {}

Example 4

Project: python-consul
License: View license
Source File: aio.py
    def __init__(
            self,
            host='127.0.0.1',
            port=8500,
            scheme='http',
            loop=None,
            verify=True):
        self.host = host
        self.port = port
        self.scheme = scheme
        self.base_uri = '%s://%s:%s' % (self.scheme, self.host, self.port)
        self._loop = loop or asyncio.get_event_loop()
        self._connector = aiohttp.TCPConnector(loop=self._loop,
                                               verify_ssl=verify)

Example 5

Project: aiogremlin
License: View license
Source File: test_sasl.py
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)
        connector = aiohttp.TCPConnector(force_close=False, loop=self.loop,
                                         verify_ssl=False)

        client_session = aiohttp.ClientSession(
            connector=connector, loop=self.loop,
            ws_response_class=GremlinClientWebSocketResponse)

        self.gc = GremlinClientSession(url="https://localhost:8182/",
                                       loop=self.loop,
                                       username="stephen", password="password",
                                       client_session=client_session)

        self.script1 = """v=graph.addVertex('name', 'Dave')"""

        self.script2 = "v.property('name')"

Example 6

Project: ProxyBroker
License: View license
Source File: providers.py
Function: start_new_session
    async def _start_new_session(self):
        # TODO: waiting aiohttp ^0.22.0 with custom resolver:
        # connector = aiohttp.TCPConnector(
        #     use_dns_cache=True, resolver=self._resolver, loop=self._loop)
        host = self.domain.split('^')[0]
        try:
            host_info = await self._resolver.resolve(host=host, family=socket.AF_INET)
        except ResolveError:
            return
        connector = aiohttp.TCPConnector(use_dns_cache=True, loop=self._loop)
        # This is a dirty hack. I know.
        connector._cached_hosts[(host, 80)] = host_info
        self._session = aiohttp.ClientSession(
            connector=connector, headers=get_headers(),
            cookies=self._cookies, loop=self._loop)

Example 7

Project: aioes
License: View license
Source File: connection.py
Function: init
    def __init__(self, endpoint, *, loop, verify_ssl=True):
        self._endpoint = endpoint
        self._session = aiohttp.ClientSession(
            connector=aiohttp.TCPConnector(
                use_dns_cache=True,
                loop=loop,
                verify_ssl=verify_ssl),
            loop=loop)
        self._base_url = '{0.scheme}://{0.host}:{0.port}/'.format(endpoint)

Example 8

Project: aiocouchdb
License: View license
Source File: client.py
    def __init__(self, *, auth=None, connector=None, loop=None):
        self._auth = auth or NoAuthProvider()

        if loop is None:
            loop = asyncio.get_event_loop()
        self._loop = loop

        if connector is None:
            self.connector = aiohttp.TCPConnector(force_close=False, loop=loop)
        else:
            self.connector = connector

Example 9

Project: threema-msgapi-sdk-python
License: View license
Source File: __init__.py
Function: init
    def __init__(self, id, secret, key=None, key_file=None, fingerprint=None,
                 verify_fingerprint=False):
        if fingerprint is None and verify_fingerprint:
            fingerprint = self.fingerprint
        connector = aiohttp.TCPConnector(fingerprint=fingerprint)
        self._session = aiohttp.ClientSession(connector=connector)
        self._key = None
        self._key_file = None
        self.id = id
        self.secret = secret
        self.key = key
        self.key_file = key_file

Example 10

Project: twtxt
License: View license
Source File: twhttp.py
def get_remote_tweets(sources, limit=None, timeout=5.0, cache=None):
    conn = aiohttp.TCPConnector(conn_timeout=timeout, use_dns_cache=True)
    headers = generate_user_agent()
    with aiohttp.ClientSession(connector=conn, headers=headers) as client:
        loop = asyncio.get_event_loop()

        def start_loop(client, sources, limit, cache=None):
            return loop.run_until_complete(process_sources_for_file(client, sources, limit, cache))

        tweets = start_loop(client, sources, limit, cache)

    return tweets

Example 11

Project: aiohttp
License: View license
Source File: test_proxy.py
    def test_proxy_connection_error(self):
        connector = aiohttp.TCPConnector(loop=self.loop)
        connector._resolve_host = make_mocked_coro(
            raise_exception=OSError('dont take it serious'))

        req = ClientRequest(
            'GET', URL('http://www.python.org'),
            proxy=URL('http://proxy.example.com'),
            loop=self.loop,
        )
        expected_headers = dict(req.headers)
        with self.assertRaises(aiohttp.ProxyConnectionError):
            self.loop.run_until_complete(connector.connect(req))
        self.assertEqual(req.url.path, '/')
        self.assertEqual(dict(req.headers), expected_headers)

Example 12

Project: gremlinclient
License: View license
Source File: client.py
    def __init__(self, url, timeout=None, username="", password="",
                 loop=None, future_class=None, connector=None):
        future_class = functools.partial(asyncio.Future, loop=loop)
        super().__init__(url, timeout=timeout, username=username,
                         password=password, loop=loop,
                         future_class=future_class)
        if connector is None:
            connector = aiohttp.TCPConnector(loop=self._loop)
        self._connector = connector

Example 13

Project: aiogremlin
License: View license
Source File: test_sasl.py
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)
        connector = aiohttp.TCPConnector(force_close=False, loop=self.loop,
                                         verify_ssl=False)

        client_session = aiohttp.ClientSession(
            connector=connector, loop=self.loop,
            ws_response_class=GremlinClientWebSocketResponse)

        self.gc = GremlinClient(url="https://localhost:8182/", loop=self.loop,
                                username="stephen", password="password",
                                client_session=client_session)

Example 14

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_ctor(loop):
    conn = aiohttp.TCPConnector(loop=loop)
    assert conn.verify_ssl
    assert conn.fingerprint is None

    with pytest.warns(DeprecationWarning):
        assert conn.resolve
    assert conn.use_dns_cache

    assert conn.family == 0

    with pytest.warns(DeprecationWarning):
        assert conn.resolved_hosts == {}
    assert conn.resolved_hosts == {}

Example 15

Project: aiohttp
License: View license
Source File: test_connector.py
def test_ambigous_verify_ssl_and_ssl_context(loop):
    with pytest.raises(ValueError):
        aiohttp.TCPConnector(
            verify_ssl=False,
            ssl_context=ssl.SSLContext(ssl.PROTOCOL_SSLv23),
            loop=loop)

Example 16

Project: aiohttp
License: View license
Source File: test_connector.py
@asyncio.coroutine
def test_tcp_connector_resolve_host_twice_use_dns_cache(loop):
    conn = aiohttp.TCPConnector(loop=loop, use_dns_cache=True)

    res = yield from conn._resolve_host('localhost', 8080)
    res2 = yield from conn._resolve_host('localhost', 8080)

    assert res is res2

Example 17

Project: aiohttp
License: View license
Source File: test_connector.py
@asyncio.coroutine
def test_tcp_connector_resolve_host_use_dns_cache(loop):
    conn = aiohttp.TCPConnector(loop=loop, use_dns_cache=True)

    res = yield from conn._resolve_host('localhost', 8080)
    assert res
    for rec in res:
        if rec['family'] == socket.AF_INET:
            assert rec['host'] == '127.0.0.1'
            assert rec['hostname'] == 'localhost'
            assert rec['port'] == 8080
        elif rec['family'] == socket.AF_INET6:
            assert rec['host'] == '::1'
            assert rec['hostname'] == 'localhost'
            assert rec['port'] == 8080

Example 18

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_clear_dns_cache(loop):
    conn = aiohttp.TCPConnector(loop=loop)
    info = object()
    conn._cached_hosts[('localhost', 123)] = info
    conn._cached_hosts[('localhost', 124)] = info
    conn.clear_dns_cache('localhost', 123)
    assert conn.cached_hosts == {('localhost', 124): info}
    conn.clear_dns_cache('localhost', 123)
    assert conn.cached_hosts == {('localhost', 124): info}
    conn.clear_dns_cache()
    assert conn.cached_hosts == {}

Example 19

Project: twtxt
License: View license
Source File: twhttp.py
def get_remote_status(sources, timeout=5.0):
    conn = aiohttp.TCPConnector(conn_timeout=timeout, use_dns_cache=True)
    headers = generate_user_agent()
    with aiohttp.ClientSession(connector=conn, headers=headers) as client:
        loop = asyncio.get_event_loop()
        result = loop.run_until_complete(process_sources_for_status(client, sources))
    return result

Example 20

Project: aiohttp
License: View license
Source File: test_connector.py
def test_both_resolve_and_use_dns_cache(loop):
    conn = aiohttp.TCPConnector(resolve=True, use_dns_cache=True,
                                loop=loop)
    assert conn.use_dns_cache
    with pytest.warns(DeprecationWarning):
        assert conn.resolve

Example 21

Project: raven-aiohttp
License: View license
Source File: raven_aiohttp.py
    def client_session(self):
        if not self.keepalive or \
                (self._client_session is None or self._client_session.closed):
            connector = aiohttp.TCPConnector(verify_ssl=self.verify_ssl,
                                             resolve=self.resolve,
                                             family=self.family,
                                             loop=self._loop)
            self._client_session = aiohttp.ClientSession(connector=connector,
                                                         loop=self._loop)
        return self._client_session

Example 22

Project: aiohttp
License: View license
Source File: test_connector.py
    def test_resolver_not_called_with_address_is_ip(self):
        resolver = unittest.mock.MagicMock()
        connector = aiohttp.TCPConnector(resolver=resolver, loop=self.loop)

        req = ClientRequest('GET',
                            URL('http://127.0.0.1:{}'.format(unused_port())),
                            loop=self.loop,
                            response_class=unittest.mock.Mock())

        with self.assertRaises(OSError):
            self.loop.run_until_complete(connector.connect(req))

        resolver.resolve.assert_not_called()

Example 23

Project: pusher-http-python
License: View license
Source File: aiohttp.py
Function: init
    def __init__(self, client):
        """Adapter for the requests module.

        :param client:  pusher.Client object
        """
        self.client = client
        self.conn = aiohttp.TCPConnector()

Example 24

Project: gremlinclient
License: View license
Source File: test_ssl.py
Function: test_submit
    def test_submit(self):
        connector = aiohttp.TCPConnector(ssl_context=sslcontext, loop=self.loop)
        @asyncio.coroutine
        def go():
            stream = yield from aiosubmit(
                "wss://localhost:8182/", "1 + 1", password="password",
                username="stephen", loop=self.loop, connector=connector)
            while True:
                msg = yield from stream.read()
                if msg is None:
                    break
                self.assertEqual(msg.status_code, 200)
                self.assertEqual(msg.data[0], 2)

        self.loop.run_until_complete(go())

Example 25

    def test_server_close_keepalive_connection(self):

        class Proto(asyncio.Protocol):

            def connection_made(self, transport):
                self.transp = transport
                self.data = b''

            def data_received(self, data):
                self.data += data
                if data.endswith(b'\r\n\r\n'):
                    self.transp.write(
                        b'HTTP/1.1 200 OK\r\n'
                        b'CONTENT-LENGTH: 2\r\n'
                        b'CONNECTION: close\r\n'
                        b'\r\n'
                        b'ok')
                    self.transp.close()

            def connection_lost(self, exc):
                self.transp = None

        @asyncio.coroutine
        def go():
            server = yield from self.loop.create_server(
                Proto, '127.0.0.1', unused_port())

            addr = server.sockets[0].getsockname()

            connector = aiohttp.TCPConnector(loop=self.loop)

            url = 'http://{}:{}/'.format(*addr)
            for i in range(2):
                r = yield from client.request('GET', url,
                                              connector=connector,
                                              loop=self.loop)
                yield from r.read()
                self.assertEqual(0, len(connector._conns))
            connector.close()
            server.close()
            yield from server.wait_closed()

        self.loop.run_until_complete(go())

Example 26

Project: waterbutler
License: View license
Source File: provider.py
Function: connector
    def connector(self):
        return aiohttp.TCPConnector(verify_ssl=self.verify_ssl)

Example 27

    def test_handle_keepalive_on_closed_connection(self):

        class Proto(asyncio.Protocol):

            def connection_made(self, transport):
                self.transp = transport
                self.data = b''

            def data_received(self, data):
                self.data += data
                if data.endswith(b'\r\n\r\n'):
                    self.transp.write(
                        b'HTTP/1.1 200 OK\r\n'
                        b'CONTENT-LENGTH: 2\r\n'
                        b'\r\n'
                        b'ok')
                    self.transp.close()

            def connection_lost(self, exc):
                self.transp = None

        @asyncio.coroutine
        def go():
            server = yield from self.loop.create_server(
                Proto, '127.0.0.1', unused_port())

            addr = server.sockets[0].getsockname()

            connector = aiohttp.TCPConnector(loop=self.loop)

            url = 'http://{}:{}/'.format(*addr)

            r = yield from client.request('GET', url,
                                          connector=connector,
                                          loop=self.loop)
            yield from r.read()
            self.assertEqual(1, len(connector._conns))

            with self.assertRaises(aiohttp.ClientError):
                yield from client.request('GET', url,
                                          connector=connector,
                                          loop=self.loop)
            self.assertEqual(0, len(connector._conns))

            connector.close()
            server.close()
            yield from server.wait_closed()

        self.loop.run_until_complete(go())

Example 28

Project: aiobotocore
License: View license
Source File: endpoint.py
Function: init
    def __init__(self, host,
                 endpoint_prefix, event_emitter, proxies=None, verify=True,
                 timeout=DEFAULT_TIMEOUT, response_parser_factory=None,
                 loop=None, connector_args=None):

        super().__init__(host, endpoint_prefix,
                         event_emitter, proxies=proxies, verify=verify,
                         timeout=timeout,
                         response_parser_factory=response_parser_factory)

        if isinstance(timeout, (list, tuple)):
            self._conn_timeout, self._read_timeout = timeout
        else:
            self._conn_timeout = self._read_timeout = timeout

        self._loop = loop or asyncio.get_event_loop()
        if connector_args is None:
            # AWS has a 20 second idle timeout:
            #   https://forums.aws.amazon.com/message.jspa?messageID=215367
            # aiohttp default timeout is 30s so set something reasonable here
            connector = aiohttp.TCPConnector(loop=self._loop,
                                             keepalive_timeout=12,
                                             conn_timeout=self._conn_timeout)
        else:
            connector = aiohttp.TCPConnector(loop=self._loop,
                                             conn_timeout=self._conn_timeout,
                                             **connector_args)

        self._aio_session = aiohttp.ClientSession(
            connector=connector,
            skip_auto_headers={'CONTENT-TYPE'},
            response_class=ClientResponseProxy, loop=self._loop)

Example 29

Project: ProxyBroker
License: View license
Source File: judge.py
    async def check(self, real_ext_ip):
        # TODO: need refactoring
        try:
            self.ip = await self._resolver.resolve(self.host)
        except ResolveError:
            return

        if self.scheme == 'SMTP':
            self.is_working = True
            self.available[self.scheme].append(self)
            self.ev[self.scheme].set()
            return

        page = False
        headers, rv = get_headers(rv=True)
        connector = aiohttp.TCPConnector(
            loop=self._loop, verify_ssl=self.verify_ssl, force_close=True)
        try:
            with aiohttp.Timeout(self.timeout, loop=self._loop),\
                 aiohttp.ClientSession(connector=connector, loop=self._loop) as session:
                async with session.get(url=self.url, headers=headers,
                                       allow_redirects=False) as resp:
                    page = await resp.text()
        except (asyncio.TimeoutError, aiohttp.ClientOSError,
                aiohttp.ClientResponseError, aiohttp.ServerDisconnectedError) as e:
            log.error('%s is failed. Error: %r;' % (self, e))
            return

        page = page.lower()

        if (resp.status == 200 and real_ext_ip in page and rv in page):
            self.marks['via'] = page.count('via')
            self.marks['proxy'] = page.count('proxy')
            self.is_working = True
            self.available[self.scheme].append(self)
            self.ev[self.scheme].set()
            log.debug('%s is verified' % self)
        else:
            log.error(('{j} is failed. HTTP status code: {code}; '
                       'Real IP on page: {ip}; Version: {word}; '
                       'Response: {page}').format(
                      j=self, code=resp.status, page=page,
                      ip=(real_ext_ip in page), word=(rv in page)))

Example 30

Project: aiorest
License: View license
Source File: cookie_session.py
def main():
    loop = asyncio.get_event_loop()

    handler = Handler()

    session_factory = CookieSessionFactory(secret_key=b'secret',
                                           cookie_name='test_cookie',
                                           dumps=json.dumps,
                                           loads=json.loads,
                                           loop=loop)

    server = aiorest.RESTServer(hostname='127.0.0.1', keep_alive=75,
                                session_factory=session_factory,
                                loop=loop)

    server.add_url('GET', '/count', handler.counter)

    srv = loop.run_until_complete(loop.create_server(
        server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        connector = aiohttp.TCPConnector(share_cookies=True, loop=loop)

        for _ in range(6):
            resp = yield from aiohttp.request(
                'GET', 'http://127.0.0.1:8080/count',
                connector=connector, loop=loop)
            data = yield from resp.json()
            print('Count is', data)

    loop.run_until_complete(query())

    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()

Example 31

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_fingerprint_invalid(loop):
    invalid = b'\x00'
    with pytest.raises(ValueError):
        aiohttp.TCPConnector(loop=loop, fingerprint=invalid)

Example 32

Project: aiorest
License: View license
Source File: redis_session.py
def main():
    loop = asyncio.get_event_loop()

    handler = Handler()

    redis = loop.run_until_complete(
        Connection.create(db=0, encoder=BytesEncoder(), loop=loop))

    session_factory = RedisSessionFactory(redis,
                                          secret_key=b'secret',
                                          cookie_name='test_cookie',
                                          loop=loop)

    server = aiorest.RESTServer(hostname='127.0.0.1', keep_alive=75,
                                session_factory=session_factory,
                                loop=loop)

    server.add_url('GET', '/count', handler.counter)

    srv = loop.run_until_complete(loop.create_server(
        server.make_handler, '127.0.0.1', 8080))

    @asyncio.coroutine
    def query():
        connector = aiohttp.TCPConnector(share_cookies=True, loop=loop)

        for _ in range(6):
            resp = yield from aiohttp.request(
                'GET', 'http://127.0.0.1:8080/count',
                connector=connector, loop=loop)
            data = yield from resp.json()
            print('Count is', data)

    loop.run_until_complete(query())

    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.close()

Example 33

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_ctor_fingerprint_valid(loop):
    valid = b'\xa2\x06G\xad\xaa\xf5\xd8\\J\x99^by;\x06='
    conn = aiohttp.TCPConnector(loop=loop, fingerprint=valid)
    assert conn.fingerprint == valid

Example 34

Project: goblin-legacy
License: View license
Source File: connection.py
Function: get_connector
def _get_connector(ssl_context):
    if _scheme in SECURE_SCHEMES:
        if ssl_context is None:
            raise ValueError("Please pass ssl_context for secure protocol")

        if _client_module == AIOHTTP_CLIENT_MODULE:
            import aiohttp
            connector = aiohttp.TCPConnector(ssl_context=ssl_context,
                                             loop=loop)

        elif _client_module == TORNADO_CLIENT_MODULE:
            from functools import partial
            from tornado import httpclient
            connector = partial(
                httpclient.HTTPRequest, ssl_options=sslcontext)
        else:
            raise ValueError("Unknown client module")
    elif _scheme in INSECURE_SCHEMES:
        connector = None
    else:
        raise ValueError("Unknown protocol")
    return connector

Example 35

Project: aiorest
License: View license
Source File: cookie_session_test.py
    def test_full_cycle(self):
        with self.run_server() as (srv, base_url):
            url = base_url + '/counter'

            @asyncio.coroutine
            def queries():
                connector = aiohttp.TCPConnector(share_cookies=True,
                                                 loop=self.loop)
                # initiate session; set start value to 2
                resp = yield from aiohttp.request('GET', url + "/2",
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 3})

                # do increment
                resp = yield from aiohttp.request('GET', url,
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 4})

                # try to override start value
                resp = yield from aiohttp.request('GET', url + '/3',
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 5})

                # session deleted; try count
                resp = yield from aiohttp.request('GET', url,
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 1})

            self.loop.run_until_complete(queries())

Example 36

Project: aiohttp
License: View license
Source File: test_connector.py
def test_tcp_connector_clear_dns_cache_bad_args(loop):
    conn = aiohttp.TCPConnector(loop=loop)
    with pytest.raises(ValueError):
        conn.clear_dns_cache('localhost')

Example 37

Project: aiohttp
License: View license
Source File: test_connector.py
def test_dont_recreate_ssl_context(loop):
    conn = aiohttp.TCPConnector(loop=loop)
    ctx = conn.ssl_context
    assert ctx is conn.ssl_context

Example 38

Project: aiohttp
License: View license
Source File: test_connector.py
def test_respect_precreated_ssl_context(loop):
    ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    conn = aiohttp.TCPConnector(loop=loop, ssl_context=ctx)
    assert ctx is conn.ssl_context

Example 39

Project: aiogremlin
License: View license
Source File: client.py
@asyncio.coroutine
def submit(gremlin, *,
           url='http://localhost:8182/',
           bindings=None,
           lang="gremlin-groovy",
           rebindings=None,
           op="eval",
           processor="",
           timeout=None,
           session=None,
           loop=None,
           conn_timeout=None,
           username="",
           password=""):
    """
    :ref:`coroutine<coroutine>`

    Submit a script to the Gremlin Server.

    :param str gremlin: The Gremlin script.
    :param str url: url for Gremlin Server (optional). 'http://localhost:8182/'
        by default
    :param dict bindings: A mapping of bindings for Gremlin script.
    :param str lang: Language of scripts submitted to the server.
        "gremlin-groovy" by default
    :param dict rebindings: Rebind ``Graph`` and ``TraversalSource``
        objects to different variable names in the current request
    :param str op: Gremlin Server op argument. "eval" by default.
    :param str processor: Gremlin Server processor argument. "" by default.
    :param float timeout: timeout for establishing connection (optional).
        Values ``0`` or ``None`` mean no timeout
    :param str session: Session id (optional). Typically a uuid
    :param loop: :ref:`event loop<asyncio-event-loop>` If param is ``None``,
        `asyncio.get_event_loop` is used for getting default event loop
        (optional)
    :param float conn_timeout: timeout for establishing connection (seconds)
        (optional). Values ``0`` or ``None`` mean no timeout
    :param username: Username for SASL auth
    :param password: Password for SASL auth
    :returns: :py:class:`aiogremlin.client.GremlinResponse` object
    """

    if loop is None:
        loop = asyncio.get_event_loop()

    connector = aiohttp.TCPConnector(force_close=True, loop=loop,
                                     verify_ssl=False,
                                     conn_timeout=conn_timeout)

    client_session = aiohttp.ClientSession(
        connector=connector, loop=loop,
        ws_response_class=GremlinClientWebSocketResponse)

    gremlin_client = GremlinClient(url=url, loop=loop,
                                   ws_connector=client_session,
                                   username=username, password=password)

    try:
        resp = yield from gremlin_client.submit(
            gremlin, bindings=bindings, lang=lang, rebindings=rebindings,
            op=op, processor=processor, session=session, timeout=timeout)

        return resp

    finally:
        gremlin_client.detach()
        client_session.detach()

Example 40

Project: plecost
License: View license
Source File: utils.py
Function: init
    def __init__(self, process_url_content, max_redirects=2, max_tries=4, max_tasks=10, loop=None,
                 connector=None):
        """
        :param process_url_content: function to process URL content, after it is downloaded
        :type process_url_content: function

        :param max_redirects: maximum number of redirects
        :type max_redirects: int

        :param max_tries: maximum number of HTTP retries.
        :type max_tries: int

        :param max_tasks: maximum number of concurrent tasks
        :type max_tasks: int

        :param loop: optional event loop object
        :type loop: loop

        :param connector: aioTCPConnector object
        :type connector: aiohttp.TCPConnector


        >>> import asyncio
        >>> display=lambda x: print(x)
        >>> loop = asyncio.get_event_loop()
        >>> v = ConcurrentDownloader(url_base="http://myhost.com", process_url_content=display)
        >>> loop.run_until_complete(v.run())
        """
        self.max_redirects = max_redirects
        self.process_url_function = process_url_content or (lambda x: None)
        self.max_tries = max_tries
        self.max_tasks = max_tasks
        self.loop = loop or asyncio.get_event_loop()
        self.q = asyncio.JoinableQueue(loop=self.loop)
        self.connector = connector or aiohttp.TCPConnector(loop=self.loop)
        self.__results = []
        self.__results_append = self.results.append

Example 41

Project: xmppwb
License: View license
Source File: bridge.py
    def _parse_outgoing_webhooks(self, bridge_cfg):
        """Parses the `outgoing webhooks` from this bridge's config file
        section.

        This also sets up the HTTP client session for each webhook."""
        if 'outgoing_webhooks' not in bridge_cfg:
            # No outgoing webhooks in this bridge.
            return

        outgoing_webhooks = bridge_cfg['outgoing_webhooks']

        for outgoing_webhook in outgoing_webhooks:
            if 'url' not in outgoing_webhook:
                raise InvalidConfigError("Error in config file: "
                                         "'url' is missing from an "
                                         "outgoing webhook definition.")

            # Set up SSL context for certificate pinning.
            if 'cafile' in outgoing_webhook:
                cafile = os.path.abspath(outgoing_webhook['cafile'])
                sslcontext = ssl.create_default_context(cafile=cafile)
                conn = aiohttp.TCPConnector(ssl_context=sslcontext)
                session = aiohttp.ClientSession(loop=self.main_bridge.loop,
                                                connector=conn)
            else:
                session = aiohttp.ClientSession(loop=self.main_bridge.loop)
            # TODO: Handle ConnectionRefusedError.
            outgoing_webhook['session'] = session

            self.outgoing_webhooks.append(outgoing_webhook)

Example 42

Project: plecost
License: View license
Source File: versions.py
Function: find_versions
def find_versions(args):
    """
    Main function to run libs as version finder.

    :param args: PlecostOptions object
    :type args: `PlecostOptions`

    :return: PlecostResults object.
    :rtype: `PlecostResults`

    :raises: PlecostTargetNotAvailable, PlecostNotWordPressFound
    """
    # --------------------------------------------------------------------------
    # Common vars
    # --------------------------------------------------------------------------
    url = args.target
    parsed_url = urlparse(args.target)
    host = parsed_url.hostname
    concurrency = args.concurrency
    log = args.log_function
    proxy = args.proxy
    is_color = args.colorize
    start_time = datetime.now()
    no_check_wordpress = args.no_check_wordpress
    no_check_plugins = args.no_check_plugins
    no_check_wordpress_version = args.no_check_wordpress_version
    force_scan = args.force_scan

    # Jackass mode is set?
    if args.jackass is True:
        concurrency = 9999

    # Non-blocking config
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    con = aiohttp.TCPConnector(conn_timeout=10, share_cookies=True, loop=loop, verify_ssl=False)
    _download = partial(download, max_redirect=0, connector=con, loop=loop)

    # Get CVE database
    db = DB(path=join(get_data_folder(), "cve.db"))

    # --------------------------------------------------------------------------
    # Test availability of target
    # --------------------------------------------------------------------------
    log("[*] Testing target connection...")
    headers, status, content = loop.run_until_complete(_download(url, method="get", get_content=False))

    # Detect redirect
    if status in (300, 301, 302, 303, 307):
        url = headers.get("location", None)

        if url is not None:
            log("\n[%s] Redirection detected to '%s'. Using it now. " % (colorize("ii", "yellow"), url),
                log_level=1)
        else:
            raise PlecostTargetNotAvailable("Redirection detected, but can't determinate the new location")
    log(colorize(" ok!\n"))

    # --------------------------------------------------------------------------
    # Check if remote host is a WordPress
    # --------------------------------------------------------------------------
    if no_check_wordpress is False:
            log("[*] Looking for WordPress installation...\n")
            # Error page content.
            headers, status, error_page = loop.run_until_complete(_download(generate_error_page(url)))

            _is_wp = loop.run_until_complete(is_remote_a_wordpress(url, error_page, _download))

            if not _is_wp:
                if force_scan is False:
                    raise PlecostNotWordPressFound("No WordPress installations found in '%s'." % host)
                else:
                    log(colorize("\n   No Wordpress installation found!\n", "yellow"))
            else:
                log("\n   %s" % colorize(" ok!\n"))

    # --------------------------------------------------------------------------
    # Check WordPress version
    # --------------------------------------------------------------------------
    if no_check_wordpress_version is False:
            log("[*] Getting WordPress version... ")

            wordpress_version = loop.run_until_complete(get_wordpress_version(url, _download, db))
            # wordpress_version.
            if wordpress_version:
                log("%s (latest: %s)" %
                    (
                        colorize("%s" % wordpress_version.current_version,
                                 "red" if wordpress_version.is_outdated is True else "blue"),
                        colorize("%s" % wordpress_version.latest_version)
                    ), 0)

                # --------------------------------------------------------------------------
                # Looking for CVEs for installed Wordpress version
                # --------------------------------------------------------------------------
                if wordpress_version.vulnerabilities:
                    log("\n    |_CVE list:\n")
                    for cve in wordpress_version.vulnerabilities:
                        log("    |__%(cve)s: (http://cve.mitre.org/cgi-bin/cvename.cgi?name=%(cve)s)\n" %
                            {"cve": colorize(cve, "red")})
                    log("\n")
            else:
                    log(colorize("Unknown!\n", "red"))

            log("\n")
    else:
        wordpress_version = PlecostWordPressInfo(last_version="",
                                                 current_version="",
                                                 vulnerabilities=[])

    # --------------------------------------------------------------------------
    # Check the plugins
    # --------------------------------------------------------------------------
    # Read plugins file and remove \n and \r
    plugins_info = []

    if no_check_plugins is False:
        plugins = []
        plugins_append = plugins.append

        with open(args.wordlist, "rU") as f:
            for plugin in f:
                plugins_append(plugin.replace("\n", "").replace("\r", ""))

        # Prepare csv file
        cve_info = csv.reader(plugins)
        error_page = ""
        # Find plugins
        log("[*] Looking for plugins (wordlist: %s) ... " % args.wordlist[args.wordlist.rfind("/") + 1:], 0)

        plugins_info = loop.run_until_complete(plugins_testing(url,
                                                               error_page,
                                                               log,
                                                               cve_info,
                                                               db,
                                                               concurrency,
                                                               loop,
                                                               con=con))
    log("\n[*] Done! \n")

    # Set finish time
    end_time = datetime.now()

    # --------------------------------------------------------------------------
    # Clean up
    # --------------------------------------------------------------------------
    con.close()

    # --------------------------------------------------------------------------
    # Make results
    # --------------------------------------------------------------------------
    return PlecostResults(target=args.target,
                          start_time=start_time,
                          end_time=end_time,
                          wordpress_info=wordpress_version,
                          plugins=plugins_info)

Example 43

Project: iamine
License: View license
Source File: core.py
    def __init__(self,
                 loop=None,
                 max_tasks=None,
                 retries=None,
                 secure=None,
                 hosts=None,
                 params=None,
                 config=None,
                 config_file=None,
                 access=None,
                 secret=None,
                 debug=None):

        # Set default values for kwargs.
        loop = asyncio.get_event_loop() if not loop else loop
        max_tasks = 100 if not max_tasks else max_tasks
        max_retries = 10 if not retries else retries
        protocol = 'http://' if not secure else 'https://'
        config = get_config(config, config_file)
        access = config.get('s3', {}).get('access', access)
        secret = config.get('s3', {}).get('secret', secret)
        debug = True if debug else False

        self.max_tasks = max_tasks
        self.max_retries = max_retries
        self.protocol = protocol
        self.hosts = hosts
        self.config = config
        self.access = access
        self.debug = debug
        self.cookies = config.get('cookies', {})

        # Asyncio/Aiohttp settings.
        self.connector = aiohttp.TCPConnector(share_cookies=True, loop=loop)
        self.connector.update_cookies(self.cookies)
        self.loop = loop
        self.q = Queue(1000, loop=self.loop)
        self.q = Queue(loop=self.loop)

        # Require valid access key!
        self.assert_s3_keys_valid(access, secret)

        # Rate limiting.
        self._max_per_second = self.get_global_rate_limit()
        self._min_interval = 1.0 / float(self._max_per_second)
        self._last_time_called = 0.0

Example 44

Project: aiohttp
License: View license
Source File: client.py
    def __init__(self, *, connector=None, loop=None, cookies=None,
                 headers=None, skip_auto_headers=None,
                 auth=None, request_class=ClientRequest,
                 response_class=ClientResponse,
                 ws_response_class=ClientWebSocketResponse,
                 version=aiohttp.HttpVersion11,
                 cookie_jar=None):

        if connector is None:
            connector = aiohttp.TCPConnector(loop=loop)
            loop = connector._loop  # never None
        else:
            if loop is None:
                loop = connector._loop  # never None
            elif connector._loop is not loop:
                raise ValueError("loop argument must agree with connector")

        self._loop = loop
        if loop.get_debug():
            self._source_traceback = traceback.extract_stack(sys._getframe(1))

        if cookie_jar is None:
            cookie_jar = CookieJar(loop=loop)
        self._cookie_jar = cookie_jar

        if cookies is not None:
            self._cookie_jar.update_cookies(cookies)
        self._connector = connector
        self._default_auth = auth
        self._version = version

        # Convert to list of tuples
        if headers:
            headers = CIMultiDict(headers)
        else:
            headers = CIMultiDict()
        self._default_headers = headers
        if skip_auto_headers is not None:
            self._skip_auto_headers = frozenset([istr(i)
                                                 for i in skip_auto_headers])
        else:
            self._skip_auto_headers = frozenset()

        self._request_class = request_class
        self._response_class = response_class
        self._ws_response_class = ws_response_class

Example 45

Project: aiohttp
License: View license
Source File: client.py
Function: request
def request(method, url, *,
            params=None,
            data=None,
            headers=None,
            skip_auto_headers=None,
            cookies=None,
            auth=None,
            allow_redirects=True,
            max_redirects=10,
            encoding='utf-8',
            version=None,
            compress=None,
            chunked=None,
            expect100=False,
            connector=None,
            loop=None,
            read_until_eof=True,
            request_class=None,
            response_class=None,
            proxy=None,
            proxy_auth=None):
    """Constructs and sends a request. Returns response object.

    method - HTTP method
    url - request url
    params - (optional) Dictionary or bytes to be sent in the query
      string of the new request
    data - (optional) Dictionary, bytes, or file-like object to
      send in the body of the request
    headers - (optional) Dictionary of HTTP Headers to send with
      the request
    cookies - (optional) Dict object to send with the request
    auth - (optional) BasicAuth named tuple represent HTTP Basic Auth
    auth - aiohttp.helpers.BasicAuth
    allow_redirects - (optional) If set to False, do not follow
      redirects
    version - Request HTTP version.
    compress - Set to True if request has to be compressed
       with deflate encoding.
    chunked - Set to chunk size for chunked transfer encoding.
    expect100 - Expect 100-continue response from server.
    connector - BaseConnector sub-class instance to support
       connection pooling.
    read_until_eof - Read response until eof if response
       does not have Content-Length header.
    request_class - (optional) Custom Request class implementation.
    response_class - (optional) Custom Response class implementation.
    loop - Optional event loop.

    Usage::

      >>> import aiohttp
      >>> resp = yield from aiohttp.request('GET', 'http://python.org/')
      >>> resp
      <ClientResponse(python.org/) [200]>
      >>> data = yield from resp.read()

    """
    warnings.warn("Use ClientSession().request() instead", DeprecationWarning)
    if connector is None:
        connector = aiohttp.TCPConnector(loop=loop, force_close=True)

    kwargs = {}

    if request_class is not None:
        kwargs['request_class'] = request_class

    if response_class is not None:
        kwargs['response_class'] = response_class

    session = ClientSession(loop=loop,
                            cookies=cookies,
                            connector=connector,
                            **kwargs)
    return _DetachedRequestContextManager(
        session._request(method, url,
                         params=params,
                         data=data,
                         headers=headers,
                         skip_auto_headers=skip_auto_headers,
                         auth=auth,
                         allow_redirects=allow_redirects,
                         max_redirects=max_redirects,
                         encoding=encoding,
                         version=version,
                         compress=compress,
                         chunked=chunked,
                         expect100=expect100,
                         read_until_eof=read_until_eof,
                         proxy=proxy,
                         proxy_auth=proxy_auth,),
        session=session)

Example 46

Project: aiohttp
License: View license
Source File: client.py
Function: ws_connect
def ws_connect(url, *, protocols=(), timeout=10.0, connector=None, auth=None,
               ws_response_class=ClientWebSocketResponse, autoclose=True,
               autoping=True, loop=None, origin=None, headers=None):

    warnings.warn("Use ClientSession().ws_connect() instead",
                  DeprecationWarning)
    if loop is None:
        loop = asyncio.get_event_loop()

    if connector is None:
        connector = aiohttp.TCPConnector(loop=loop, force_close=True)

    session = aiohttp.ClientSession(loop=loop, connector=connector, auth=auth,
                                    ws_response_class=ws_response_class,
                                    headers=headers)

    return _DetachedWSRequestContextManager(
        session._ws_connect(url,
                            protocols=protocols,
                            timeout=timeout,
                            autoclose=autoclose,
                            autoping=autoping,
                            origin=origin),
        session=session)

Example 47

Project: aiohttp
License: View license
Source File: async.py
Function: run
@asyncio.coroutine
def run(test, count, concurrency, *, loop, verbose, profile):
    if verbose:
        print("Prepare")
    else:
        print('.', end='', flush=True)
    host, port = find_port()
    barrier = Barrier(2)
    server = Process(target=test, args=(host, port, barrier, profile))
    server.start()
    barrier.wait()

    url = 'http://{}:{}'.format(host, port)

    connector = aiohttp.TCPConnector(loop=loop)
    with aiohttp.ClientSession(connector=connector) as client:

        for i in range(10):
            # make server hot
            resp = yield from client.get(url+'/prepare')
            assert resp.status == 200, resp.status
            yield from resp.release()

        if verbose:
            test_name = test.__name__
            print("Attack", test_name)
        rps, data = yield from attack(count, concurrency, client, loop, url)
        if verbose:
            print("Done")

        resp = yield from client.get(url+'/stop')
        assert resp.status == 200, resp.status
        yield from resp.release()

    server.join()
    return rps, data

Example 48

Project: goblin
License: View license
Source File: connection.py
Function: open
    async def open(cls, url, loop, *, ssl_context=None, username='',
                   password='', max_inflight=64, response_timeout=None,
                   message_serializer=serializer.GraphSON2MessageSerializer):
        """
        **coroutine** Open a connection to the Gremlin Server.

        :param str url: url for host Gremlin Server
        :param asyncio.BaseEventLoop loop:
        :param ssl.SSLContext ssl_context:
        :param str username: Username for database auth
        :param str password: Password for database auth

        :param int max_inflight: Maximum number of unprocessed requests at any
            one time on the connection
        :param float response_timeout: (optional) `None` by default

        :returns: :py:class:`Connection<goblin.driver.connection.Connection>`
        """
        connector = aiohttp.TCPConnector(ssl_context=ssl_context, loop=loop)
        client_session = aiohttp.ClientSession(loop=loop, connector=connector)
        ws = await client_session.ws_connect(url)
        return cls(url, ws, loop, client_session, username, password,
                   max_inflight, response_timeout, message_serializer)

Example 49

Project: aiohttp
License: View license
Source File: fake_server.py
Function: main
async def main(loop):
    token = "ER34gsSGGS34XCBKd7u"

    fake_facebook = FakeFacebook(loop=loop)
    info = await fake_facebook.start()
    resolver = FakeResolver(info, loop=loop)
    connector = aiohttp.TCPConnector(loop=loop, resolver=resolver,
                                     verify_ssl=False)

    async with aiohttp.ClientSession(connector=connector,
                                     loop=loop) as session:
        async with session.get('https://graph.facebook.com/v2.7/me',
                               params={'access_token': token}) as resp:
            print(await resp.json())

        async with session.get('https://graph.facebook.com/v2.7/me/friends',
                               params={'access_token': token}) as resp:
            print(await resp.json())

    await fake_facebook.stop()

Example 50

    def test_session_close(self):
        conn = aiohttp.TCPConnector(loop=self.loop)

        with run_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request(
                    'get', httpd.url('keepalive') + '?close=1',
                    connector=conn, loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.json())
            self.assertEqual(content['content'], 'requests=1')
            r.close()

            r = self.loop.run_until_complete(
                client.request('get', httpd.url('keepalive'),
                               connector=conn, loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.json())
            self.assertEqual(content['content'], 'requests=1')
            r.close()

        conn.close()
See More Examples - Go to Next Page
Page 1 Selected Page 2