Asynchronous HTTP Requests using Python’s asynchat

Watch it together with the written tutorial to deepen your understanding: Making HTTP Requests With Python

First things first: let’s install the asyncio library (if it isn’t already installed). Just add this line at the top of your script and you’ll be good to go:

# Import the asyncio library to enable asynchronous programming
import asyncio

# Define a function to make an HTTP request using the GET method
async def make_request(url):
    # Use the asyncio library to create a TCP connection to the specified URL
    reader, writer = await asyncio.open_connection(url, 80)
    # Send a GET request to the server
    writer.write(b"GET / HTTP/1.1\r\nHost: " + url.encode() + b"\r\n\r\n")
    # Wait for the server to respond
    response = await reader.read()
    # Close the connection
    writer.close()
    # Return the response from the server
    return response

# Define a function to run the program
async def main():
    # Specify the URL to make the request to
    url = "www.example.com"
    # Make the HTTP request and store the response
    response = await make_request(url)
    # Print the response from the server
    print(response)

# Run the program
asyncio.run(main())

Now that we have our trusty sidekick, let’s learn how to make some asynchronous requests using aiohttp. This library is like the Batman to asyncio’s Robin they work together seamlessly to get things done.
To use aiohttp with asyncio, you can import it like this:

# Importing necessary libraries
import asyncio # Importing the asyncio library for asynchronous programming
from aiohttp import ClientSession # Importing the ClientSession class from aiohttp library for making HTTP requests

# Defining a function to make asynchronous requests
async def make_requests(url): # Using the async keyword to define an asynchronous function
    async with ClientSession() as session: # Using the async keyword to define a context manager for the ClientSession class
        async with session.get(url) as response: # Using the async keyword to define a context manager for the get() method of ClientSession class
            return await response.text() # Using the await keyword to wait for the response and return the text content

# Defining a function to run the asynchronous requests
async def run_requests(): # Using the async keyword to define an asynchronous function
    urls = ["https://www.google.com", "https://www.facebook.com", "https://www.twitter.com"] # Creating a list of URLs to make requests to
    tasks = [] # Creating an empty list to store the tasks
    for url in urls: # Using a for loop to iterate through the URLs
        task = asyncio.create_task(make_requests(url)) # Using the create_task() method to create a task for each URL and adding it to the tasks list
        tasks.append(task) # Appending the task to the tasks list
    responses = await asyncio.gather(*tasks) # Using the await keyword to wait for all the tasks to complete and gather the responses
    print(responses) # Printing the responses

# Running the asynchronous requests
asyncio.run(run_requests()) # Using the run() method to run the asynchronous function

Now that we have our tools in hand, let’s make some requests! Here’s an example of how to send a GET request using asyncio and aiohttp:

# This function uses the asyncio library to make an asynchronous GET request to the specified URL and returns the response data as a string.
async def get_data(url):
    # The "async with" statement allows for the use of asynchronous context managers, in this case, the ClientSession() object.
    async with ClientSession() as session:
        # The "await" keyword is used to pause the execution of the function until the specified coroutine (session.get(url)) is complete.
        response = await session.get(url)
        # The "await" keyword is used again to pause the execution of the function until the specified coroutine (response.text()) is complete.
        data = await response.text()
        # The data is then returned as a string.
        return data

This function takes in the URL you want to fetch and returns the text content of that page. The `async with` statement is like a try-with-resources block for asynchronous code it ensures that the session object gets closed automatically when we’re done using it.
To run this function, simply call it from another asyncio coroutine:

# Import the necessary libraries
import asyncio
from aiohttp import ClientSession

# Define a function to fetch and return the text content of a given URL
async def get_data(url):
    # Use the `async with` statement to ensure the session object gets closed automatically
    async with ClientSession() as session:
        # Use the `get` method to make a GET request to the given URL
        async with session.get(url) as response:
            # Use the `text` method to get the text content of the response
            data = await response.text()
            # Return the data
            return data

# Define the main function to run the program
async def main():
    # Define the URL to fetch data from
    url = 'https://www.example.com/'
    # Call the `get_data` function and await its response
    data = await get_data(url)
    # Print the data
    print(data)

# Get the event loop and run the main function
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

This code sets up a loop, runs the `main` function asynchronously using that loop, and then waits for it to complete before exiting. The `async def` syntax tells Python that this is an asyncio coroutine a piece of code that can be executed asynchronously alongside other coroutines in the same event loop.
That’s all there is to it! With just a few lines of code, you can make asynchronous HTTP requests using Python’s asyncio library and aiohttp.

SICORPS