Random File Server with Bottle Framework

  • Share this:

Code introduction


This function uses the Bottle framework to randomly select a file from a specified directory and return it.


Technology Stack : Bottle, static file serving

Code Type : Web Server

Code Difficulty : Intermediate


                
                    
import random
from bottle import request, response, static_file

def random_file_server(directory="/path/to/static"):
    """
    This function serves a random file from a specified directory using the Bottle framework.
    """
    # Generate a random file name from the directory
    files = static_file.get_files(directory)
    random_file = random.choice(files)
    
    # Return the file as a static file response
    return static_file serving=random_file

# Code Information