You can download this code by clicking the button below.
This code is now available for download.
This function generates a random user agent string and sets it in the HTTP response header, returning a text response that includes the new user agent.
Technology Stack : Sanic, fake_useragent
Code Type : Sanic routing handler
Code Difficulty : Intermediate
def random_user_agent(response):
import random
from sanic import response as sanic_response
from fake_useragent import UserAgent
user_agent = UserAgent().random
response.headers['User-Agent'] = user_agent
return sanic_response.text('User-Agent set to ' + user_agent)