Getting Started with Bottle: Installation, HelloWorld, Dynamic Routing, File Download, and POST Handling
This tutorial introduces the lightweight Python Bottle framework, covering installation via pip or source, creating a simple HelloWorld server, using static and dynamic routes, serving files, handling POST requests and file uploads, and tips for local and network deployment.
Bottle is an ultra‑lightweight Python web framework consisting of a single ~4,000‑line file with no external dependencies, making it easy to install and use.
1. Installing Bottle
Use pip install bottle or download the bottle.py file directly from the GitHub repository (https://github.com/bottlepy/bottle/blob/master/bottle.py).
2. HelloWorld Example
Import get and run from Bottle, define a function decorated with @get('/') that returns a simple string, and start the server with run(host='127.0.0.1', port=80). Execute the script with python HelloWorld.py and visit http://127.0.0.1 in a browser.
3. Static Routing and File Download
Static routes are defined with @get('/'). For serving files, use static_file(filename, root='path/to/files'). The example shows a file store1.txt placed alongside HelloWorld.py, which can be accessed via the browser.
4. Dynamic Routing
Dynamic routes are created with @route('/<name>'), where the placeholder name becomes a function argument. This allows handling variable URLs and serving different content based on the path.
5. POST Requests and File Upload
To handle POST data, import post and request. Use a HTML <form> with method='POST' and enctype='multipart/form-data'. In the handler, retrieve form fields with request.forms.get('field_name') and uploaded files with request.files.get('file_field'), then save the file.
After uploading, the file appears in the server’s directory, confirming a successful POST handling.
6. Running and Deploying
Enable automatic reloading with run(reloader=True) for hot updates. To expose the server beyond localhost, replace the host IP with your public IP (e.g., from ipconfig) and ensure the port (default 80) is open. Then access the site from any device on the network.
Bottle also supports cookies, plugins, multithreading, and can be deployed on various servers, offering many advanced features for further exploration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
