Skip to main content

Using Flask as a lightweight HTTP server in place of Python's built-in http.server

Project description

simple_flask_server

PyPI version License: MIT

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.html or index.htm is 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]

You can also run it as a Python module:

python -m 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/w

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

simple_flask_server-0.0.6.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_flask_server-0.0.6-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_flask_server-0.0.6.tar.gz.

File metadata

  • Download URL: simple_flask_server-0.0.6.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for simple_flask_server-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7522a2fa890db715ad2838dcc308e15b983884151d1720c8565fcac43c8b7cbd
MD5 7b93c89e0ca372f8eabf5ae45e445d00
BLAKE2b-256 e5281466b1e502c6add4390481016878c33eed8c46e170bdc64c498d897a2702

See more details on using hashes here.

File details

Details for the file simple_flask_server-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_flask_server-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d9a494e28df5d92f0e136a6ab76862f27a8ad4589dc04620a6fde555cae98e5e
MD5 f349232dc909112e7902dea5e247e3b1
BLAKE2b-256 e480f3db7e480f94dc8dc3e98bbd8c8976ddb6b9a90701f3893b448d8001b252

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page