Using Flask as a lightweight HTTP server in place of Python's built-in http.server
Project description
simple_flask_server
A Flask equivalent of python -m http.server.
This package provides a simple HTTP server that serves files from the current directory or a specified directory, just like Python's built-in http.server module, but implemented with Flask.
Features
- Serves files from a specified directory.
- Lists directory contents if
index.htmlorindex.htmis not found. - Redirects to add a trailing slash for directories.
- Extends server functionality by loading external Python files.
- Dynamically adds routes from a command-line string.
Installation
It is recommended to use uv for installation.
uv tool install simple_flask_server
Alternatively, you can use pip:
pip install simple_flask_server
Usage
Run the server from the command line:
simple-flask-server [path] [options]
Arguments
path(optional): The directory to serve files from. If not specified, it defaults to the current working directory.
Options
-h, --help: Show this help message and exit.--bind BIND,-b BIND: Specify the address and port to bind to (e.g.,0.0.0.0:8080).--ext EXT,-e EXT: Execute a Python file in the context of the Flask app. This allows for adding custom routes. Can be specified multiple times.--cmd CMD [CMD ...],-c CMD [CMD ...]: Execute a program passed in as a string.--open,-o: Open the default web browser to the server's address.
Examples
To serve files from the current directory on the default address and port:
simple-flask-server
To serve files from a specific directory (e.g., /var/www):
simple-flask-server /var/www
To run the server on a different address and port (e.g., all interfaces on port 8080):
simple-flask-server -b 0.0.0.0:8080
To open the browser automatically:
simple-flask-server -o
Extending the Server
You can extend the server with custom Python code or external commands.
Using --ext
Specify a Python file to execute. The file is executed in the context of the Flask application, allowing you to define new routes. This option can be used multiple times.
Example:
The samples/json-entry.py file provides a simple example of a custom endpoint:
@app.route('/sample-api', methods=["POST"])
def post1():
print(dict(request.json))
return {"hello":"world"}
To run the server and load this extension:
simple-flask-server -e samples/json-entry.py
You can then send requests to the new endpoint:
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://127.0.0.1:8001/sample-api
The server will respond with:
{
"hello": "world"
}
Using --cmd
Execute a Python script string. This is useful for defining simple, dynamic routes on the fly.
Example:
simple-flask-server \
-c "@app.route('/api', methods=['POST'])
def api():
print(dict(request.json))
return {'hello':'world'}
"
More concisely:
simple-flask-server -c "app.route('/api', methods=['POST'])(lambda : {'hello':'world'})"
You can then call this new endpoint:
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"value"}' http://127.0.0.1:8001/api
The server will respond with:
{
"hello": "world"
}
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Atsuo Ishimoto
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_flask_server-0.0.7.tar.gz.
File metadata
- Download URL: simple_flask_server-0.0.7.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c3af6ef4d8ab3a331318d9d23b7873e20a579baeb87a71841eee1bbf0894bfe
|
|
| MD5 |
7e36ff4b0e97fb3324eb0cf4d8d1109c
|
|
| BLAKE2b-256 |
592a11a6bc479763491cc48897c14119e124eaac951c2db50d3febb1b6a91c2b
|
File details
Details for the file simple_flask_server-0.0.7-py3-none-any.whl.
File metadata
- Download URL: simple_flask_server-0.0.7-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
590917d9a6bea24bb936579fe8d7ddc75f21376c24a1b814c448ad7bf0292b54
|
|
| MD5 |
db5d188b62c705b5885dd0517130133a
|
|
| BLAKE2b-256 |
dff43d9481bb06b32e6741d235550818406580ac10fc986c0b37de5486411d74
|