Bottle Web App for Generating Random Strings

  • Share this:

Code introduction


This function creates a Bottle web application that accepts an HTTP GET request with a parameter `length`, which indicates the length of the random string to be generated. The function uses the `generate_random_string` function to generate the random string and returns it as the response.


Technology Stack : Bottle, Python built-in libraries (random, string)

Code Type : Web Application

Code Difficulty : Intermediate


                
                    
from bottle import request, response, route, run, template

def generate_random_string(length):
    import random
    import string
    letters = string.ascii_letters
    return ''.join(random.choice(letters) for i in range(length))

@route('/random_string/<length>')
def get_random_string(length):
    random_string = generate_random_string(int(length))
    response.content_type = 'text/plain'
    return random_string