Skip to content

MichaelCurrin/python-webserver-quickstarts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Web server Quickstarts

Run a "Hello World" webserver in a selection of Python frameworks

GitHub tag License

Made with Python

dependency - flask dependency - django dependency - cherrypy dependency - tornado

The library badges above will take you to PyPi if you click them.

This project includes short scripts for basic web servers in various frameworks. These can be used as references for comparison or interest. They can also be run, after installing server-specific dependencies.

Purpose

  • Reference of how to setup a basic server to help with other projects.
  • Run each locally as a demo.
  • Optionally update it locally to see what changes, like adding an endpoint.

Approaches

The idea is to cover a mixture of approaches.

  • HTML templating
    • Use Jinja or similar templating engine to put data in HTML page and returned the finished result to the client.
    • Model View Controller for more complex project with a database.
  • Backend and frontend.
    • Have an API endpoint of JSON data.
    • Setup a page with outline and no data.
    • Pull in the data from the API using JavaScript.

Frameworks

The scripts in this repo mostly come from quickstart guides on the official docs or blog tutorials.

Whatever framework you choose, you can use it for your hobby or production app but you'll probably want to setup a load balanced like Nginx in front of it. The frameworks tend to recommend this in your docs.

You might also want to reduce resources by implementing caching one of these:

  • Database queries (reduce load on database).
  • Endpoint or page responses (reduce load on Python app).
  • Nginx cache settings.
  • Add AWs CloudFront with caching.

Flask

  • A micro-framework with this light and easy to extend with your own code or Flask extensions (separate libraries).
  • Great for beginners.
  • Build a backend API - handle and return requests typically using RESTful GET and POST endpoints and JSON data.
  • Build a templating app.
    • Use a database on the backend and HTML on the frontend.
    • Use the Liquid / Jinja templating engine to read HTML templates and render them using loops and reusable "macros" (functions defined as Liquid).
  • Handles connecting to a database like SQLite, MySQL or Postgres. For larger projects, you might want to install a package that you choose as your database ORM - like SQLAlchemy or SQLObject.

Links:

Django

  • An opinionated framework that is built to handle a database, templating, admin view and auth without installing further libraries yourself.
  • Django is not powerful but complex so not beginner-friendly.
  • Additional Django libraries are available as replacements to handle things like GraphQL or ecommerce.

Links:

CherryPy

Not as famous as Flask, but it is similar and from some research it is supposed to be faster too.

Links:

Tornado

Links:

Requirements

  • Python >= 3.6

Installation

Note: This instructions to install and run for a Linux and macOS environments.

Follow this guide to install/update Python and install project dependencies in a new virtual environment using requirements.txt file.

Run

Start

$ cd webservers

Run a server using one of the commands below. Note that most of the files are executable so do not need as python command preceding them.

  • No framework
    $ ./no_framework_hello_world.py
  • Flask
    $ # Recommended way.
    $ FLASK_APP=flask_hello_world.py flask run
    $ # This also works:
    $ python flask_hello_world.py
  • Django
    $ cd django_hello_world
    $ python manage.py runserver 5000
    $ # For help on available commands:
    $ python manage.py help
  • CherryPy
    $ cd cherrypy_hello_world
    $ hello_world/main.py
    
    $ multiple_endpoints/main.py
  • Tornado
    $ ./tornado_hello_world.py

Press CTRL+C when you want to stop the server.

View

View the hello world response in the browser.

For Django, you also get an admin view.

Resources

See also this site which includes tutorials for Python webserver frameworks - fullstackpython.com/web-frameworks.html.

There instructions were followed to create the base Django project initially, before customizing it. Run these to get a fresh app setup.

$ pip install django

$ django-admin startproject hello_app
$ cd hello_app
$ python manage.py startapp hello

License

Released under MIT by @MichaelCurrin.