Questions tagged [aiohttp]

Use this tag for questions about AIOHTTP – a client/server framework for asyncio Python.

AIOHTTP is an asynchronous asyncio Python web framework for both client and server sides. It supports HTTP, WebSockets, and also MiddleWare and Signals mechanics for the server side.

Useful links:

Related tags:

1628 questions
12
votes
2 answers

RuntimeError: Cannot close a running event loop

I'm trying to resolve this error: RuntimeError: Cannot close a running event loop in my asyncio process. I believe it's happening because there's a failure while tasks are still pending, and then I try to close the event loop. I'm thinking I need to…
hyphen
  • 2,368
  • 5
  • 28
  • 59
12
votes
4 answers

How to run an aiohttp server in a thread?

This example of aiohttp server in a thread fails with an RuntimeError: There is no current event loop in thread 'Thread-1'. error: import threading from aiohttp import web def aiohttp_server(): def say_hello(request): return…
bux
  • 7,087
  • 11
  • 45
  • 86
12
votes
3 answers

How to await method in loop?

I have the following method that generates data for me: async def generate_url(self, video_id): data = await self.s3.generate_presigned_url(...video_id...) return data def convert_to_json(self, urls): ids = [self.generate_url(url) for…
user6611764
12
votes
1 answer

aiohttp and client-side SSL certificates

I recently moved off from flask + requests onto aiohttp and its async http client. In my scenario, I need to make a call to an API over HTTPS (with custom certificates) AND send a client-side certificate along. For the first part (validating custom…
Seeker89
  • 282
  • 1
  • 2
  • 9
12
votes
4 answers

Python asyncio: how to mock __aiter__() method?

I have a code which is listening to messages on WebSocket using aiohttp. It looks like: async for msg in ws: await self._ws_msg_handler.handle_message(ws, msg, _services) Where ws is an instance of aiohttp.web.WebSocketResponse() (original…
11
votes
3 answers

how to get response_time and response_size while using aiohttp

Is it possible to get response time and response size for each request made using aiohttp? The documentation seems not to have those properties anywhere. Thanks
Dan
  • 2,209
  • 3
  • 23
  • 44
11
votes
1 answer

Send user credentials in aiohttp request

I can't find a way to send user credentials with aiohttp. I want similar behavior to cURL's curl --user "USER:PASSWORD" but in aiohttp. On the reference doc I can't find this option, I can find query parameters, headers, body, but not user…
Reda Drissi
  • 1,532
  • 3
  • 20
  • 32
11
votes
1 answer

Writing unit tests when using aiohttp and asyncio

I am updating one of my Python packages so it is asynchronous (using aiohttp instead of requests). I am also updating my unit tests so they work with the new asynchronous version, but I'm having some trouble with this. Here is a snippet from my…
Amos
  • 1,154
  • 1
  • 16
  • 35
11
votes
1 answer

Fetching multiple urls with aiohttp in python

In a previous question, a user suggested the following approach for fetching multiple urls (API calls) with aiohttp: import asyncio import aiohttp url_list =…
Jannik
  • 217
  • 1
  • 2
  • 13
11
votes
1 answer

How to pass additional parameters to handle_client coroutine?

The recommended way to use asyncio for a socket server is: import asyncio async def handle_client(reader, writer): request = (await reader.read(100)).decode() response = "Data received." writer.write(response.encode()) async def…
jsstuball
  • 4,104
  • 7
  • 33
  • 63
11
votes
6 answers

How to mock AWS S3 with aiobotocore

I have a project that uses aiohttp and aiobotocore to work with resources in AWS. I am trying to test class that works with AWS S3 and I am using moto to mock AWS. Mocking works just fine with examples that use synchronous code (example from moto…
Belerafon
  • 488
  • 4
  • 13
11
votes
1 answer

async - sync - async calls in one python event loop

Let's say I have a class which uses asyncio loop internally and doesn't have async interface: class Fetcher: _loop = None def get_result(...): """ After 3 nested sync calls async tasks are finally called with…
dzmitry
  • 381
  • 1
  • 2
  • 16
11
votes
2 answers

How to determine which port aiohttp selects when given port=0

When I use aiohttp.web.run_app(. . ., port=0), I assume that it selects an arbitrary available port on which to serve. Is this correct? And if so, is there some way to figure out what port it's selected?
abingham
  • 1,294
  • 1
  • 10
  • 17
11
votes
2 answers

Python lib beautiful soup using aiohttp

someone know how to do : import html5lib import urllib from bs4 import BeautifulSoup soup = BeautifulSoup(urllib.request.urlopen('http://someWebSite.com').read().decode('utf-8'), 'html5lib') using aiohttp instead of urllib ? Thanks ^^
APianist
  • 291
  • 1
  • 4
  • 14
10
votes
1 answer

How can I have a synchronous facade over asyncpg APIs with Python asyncio?

Imagine an asynchronous aiohttp web application that is supported by a Postgresql database connected via asyncpg and does no other I/O. How can I have a middle-layer hosting the application logic, that is not async? (I know I can simply make…
fpbhb
  • 1,469
  • 10
  • 22