Example #1
0
async def post(session, url, data=None, headers=None):
    async with session.post(url, data=data, headers=headers) as response:
        if response.status != 200:
            raise aiohttp.ClientConnectionError(
                f"Could not post to {url}.") from None
        text = await response.text()
        return text.encode().decode("unicode-escape")
Example #2
0
async def fetch(session, url, params=None):
    async with session.get(url, params=params) as response:
        if response.status != 200:
            raise aiohttp.ClientConnectionError(
                f"Could not fetch from {url}.") from None
        text = await response.text()
        return text.encode().decode("unicode-escape")
Example #3
0
async def read(self, n: int = -1) -> bytes:
    """
    Read up to 'n' bytes of the response payload.

    If 'n' is -1 (default), read the entire payload.
    """
    if self._body is None:
        try:
            if n is -1:
                self._body = await self.content.read()
            else:
                chunks = []
                i = 0
                while i < n:
                    chunk = await self.content.read(n=n - i)
                    if not chunk:
                        break
                    chunks.append(chunk)
                    i += len(chunk)

                self._body = b''.join(chunks)

            for trace in self._traces:
                await trace.send_response_chunk_received(self._body)

        except BaseException:
            self.close()
            raise
    elif self._released:
        raise aiohttp.ClientConnectionError('Connection closed')

    return self._body
Example #4
0
async def test_fetch_client_error_async(dummy_endpoint):
    with aioresponses() as m:
        async with AsyncSession() as session:
            m.post(dummy_endpoint, exception=aiohttp.ClientConnectionError())
            rqst = Request(session, 'POST', '/')
            with pytest.raises(BackendClientError):
                async with rqst.fetch():
                    pass
 async def receive(self) -> Dict[str, Any]:
     ws_msg: aiohttp.WSMessage = await self._connection.receive()
     if ws_msg.type == aiohttp.WSMsgType.CLOSED:
         self.logger().warning("Websocket server closed the connection")
         raise aiohttp.ClientConnectionError(
             "Websocket server closed the connection")
     elif ws_msg.type == aiohttp.WSMsgType.BINARY:
         raw_msg: bytes = ws_msg.data
         self._last_recv_time = self._time()
         return self.parse_message(raw_msg)
Example #6
0
    async def test_get_security_list(self):
        secs = await get_security_list()
        expected = [
            "000001.XSHE", "平安银行", "PAYH", "1991-04-03", "2200-01-01", "stock"
        ]

        self.assertListEqual(expected, secs[0].tolist())

        # test if server is down
        with mock.patch("aiohttp.ClientSession.get",
                        side_effect=aiohttp.ClientConnectionError()):
            self.assertIsNone(await get_security_list())
Example #7
0
 async def _req(self, method, path, params={}, **query):
     await self.login_if_needed()
     for i in range(self.tries):
         url = self.get_url(path, **query)
         try:
             resp = await method(url, timeout=self.timeout, **params)
             if await self._process_resp(resp):
                 return resp
             else:
                 continue
         except aiohttp.ClientConnectionError:
             pass
     raise aiohttp.ClientConnectionError('Emby server is probably down')
Example #8
0
    def _send(self, **kwargs):
        try:
            log.debug(
                "Sending message to %s serial %s: %s",
                self._url,
                Lazy(lambda: str(kwargs.get('serial'))),
                Lazy(lambda: str(kwargs))
              )

            if self.socket.closed:
                raise aiohttp.ClientConnectionError('Connection was closed.')

            send_coro = self.socket.send_json(kwargs, dumps=lambda x: json.dumps(serializer(x)))
            return self._loop.create_task(send_coro)
        except aiohttp.WebSocketError as ex:
            self._loop.create_task(self.close())
            future = self._loop.create_future()
            future.set_exception(ex)
            return future
Example #9
0
def request(method, url, *,
            params=None,
            data=None,
            headers=None,
            cookies=None,
            files=None,
            auth=None,
            allow_redirects=True,
            max_redirects=10,
            encoding='utf-8',
            version=(1, 1),
            timeout=None,
            compress=None,
            chunked=None,
            expect100=False,
            connector=None,
            loop=None,
            read_until_eof=True,
            request_class=None):
    """Constructs and sends a request. Returns response object.

    :param method: http method
    :param url: request url
    :param params: (optional) Dictionary or bytes to be sent in the query
      string of the new request
    :param data: (optional) Dictionary, bytes, or file-like object to
      send in the body of the request
    :param headers: (optional) Dictionary of HTTP Headers to send with
      the request
    :param cookies: (optional) Dict object to send with the request
    :param files: (optional) Dictionary of 'name': file-like-objects
       for multipart encoding upload
    :param auth: (optional) Auth tuple to enable Basic HTTP Auth
    :param timeout: (optional) Float describing the timeout of the request
    :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE
       redirect following is allowed.
    :param compress: Boolean. Set to True if request has to be compressed
       with deflate encoding.
    :param chunked: Boolean or Integer. Set to chunk size for chunked
       transfer encoding.
    :param expect100: Boolean. Expect 100-continue response from server.
    :param connector: aiohttp.conntect.SocketConnector instance to support
       connection pooling and session cookies.
    :param read_until_eof: Read response until eof if response
       does not have Content-Length header.
    :param request_class: Custom Request class implementation.
    :param loop: Optional event loop.

    Usage::

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

    """
    redirects = 0
    if loop is None:
        loop = asyncio.get_event_loop()
    if request_class is None:
        request_class = HttpRequest
    if connector is None:
        connector = aiohttp.SocketConnector(loop=loop)

    while True:
        req = request_class(
            method, url, params=params, headers=headers, data=data,
            cookies=cookies, files=files, auth=auth, encoding=encoding,
            version=version, compress=compress, chunked=chunked,
            loop=loop, expect100=expect100)

        try:
            conn = yield from connector.connect(req)

            resp = req.send(conn.writer, conn.reader)
            try:
                if timeout:
                    yield from asyncio.wait_for(
                        resp.start(conn, read_until_eof), timeout, loop=loop)
                else:
                    yield from resp.start(conn, read_until_eof)
            except:
                resp.close()
                conn.close()
                raise
        except asyncio.TimeoutError:
            raise aiohttp.TimeoutError from None
        except aiohttp.BadStatusLine as exc:
            raise aiohttp.ClientConnectionError(exc)
        except OSError as exc:
            raise aiohttp.OsConnectionError(exc)

        # redirects
        if resp.status in (301, 302) and allow_redirects:
            redirects += 1
            if max_redirects and redirects >= max_redirects:
                resp.close()
                break

            r_url = resp.get('location') or resp.get('uri')

            scheme = urllib.parse.urlsplit(r_url)[0]
            if scheme not in ('http', 'https', ''):
                raise ValueError('Can redirect only to http or https')
            elif not scheme:
                r_url = urllib.parse.urljoin(url, r_url)

            url = urllib.parse.urldefrag(r_url)[0]
            if url:
                resp.close()
                continue

        break

    return resp
Example #10
0
async def jfetch(session, url, params=None):
    async with session.get(url, params=params) as response:
        if response.status != 200:
            raise aiohttp.ClientConnectionError(
                f"Could not fetch from {url}.") from None
        return await response.json()