File: aiohttp_client.py

package info (click to toggle)
python-pook 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 3,558; makefile: 13
file content (31 lines) | stat: -rw-r--r-- 820 bytes parent folder | download
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
# flake8: noqa

import pook
import aiohttp
import asyncio
import async_timeout


async def fetch(session, url, data):
    with async_timeout.timeout(10):
        async with session.get(url, data=data) as res:
            print("Status:", res.status)
            print("Headers:", res.headers)
            print("Body:", await res.text())


with pook.use(network=True):
    pook.get(
        "http://httpbin.org/ip",
        reply=404,
        response_type="json",
        response_headers={"Server": "nginx"},
        response_json={"error": "not found"},
    )

    async def main(loop):
        async with aiohttp.ClientSession(loop=loop) as session:
            await fetch(session, "http://httpbin.org/ip", bytearray("foo bar", "utf-8"))

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(loop))