Asynchronous User Information Fetching and Display

  • Share this:

Code introduction


This code defines an asynchronous function that fetches user information from a random user API and prints it out.


Technology Stack : Aiohttp-web, Python asynchronous programming

Code Type : Asynchronous function

Code Difficulty : Intermediate


                
                    
import aiohttp
import asyncio

async def fetch_random_user(session):
    url = "https://randomuser.me/api/"
    async with session.get(url) as response:
        data = await response.json()
        return data['results'][0]

async def print_user_info():
    async with aiohttp.ClientSession() as session:
        user_info = await fetch_random_user(session)
        print(f"Name: {user_info['name']['first']} {user_info['name']['last']}")
        print(f"Email: {user_info['email']}")
        print(f"Phone: {user_info['phone']}")