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
Metadata
Release files for simple-flask-server 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simple_flask_server-0.0.7.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_flask_server-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.1 kB
Release files / simple_flask_server-0.0.7.tar.gz
| Download URL | simple_flask_server-0.0.7.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c3af6ef4d8ab3a331318d9d23b7873e20a579baeb87a71841eee1bbf0894bfe
|
|
BLAKE2b-256 checksum How to use checksums |
592a11a6bc479763491cc48897c14119e124eaac951c2db50d3febb1b6a91c2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|
Release files / simple_flask_server-0.0.7-py3-none-any.whl
| Download URL | simple_flask_server-0.0.7-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
590917d9a6bea24bb936579fe8d7ddc75f21376c24a1b814c448ad7bf0292b54
|
|
BLAKE2b-256 checksum How to use checksums |
dff43d9481bb06b32e6741d235550818406580ac10fc986c0b37de5486411d74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.13
|