Random User Agent Generator Using Sanic and fake_useragent

  • Share this:

Code introduction


This function uses the Sanic web framework and the fake_useragent library to generate a random user agent string and return it.


Technology Stack : Sanic, fake_useragent

Code Type : Web Application

Code Difficulty : Intermediate


                
                    
def random_user_agent(request):
    import random
    from sanic import response
    from fake_useragent import UserAgent

    # Generate a random user agent string
    user_agent = UserAgent().random

    # Create a response with the user agent
    return response.text(f'Random User Agent: {user_agent}', content_type='text/plain')