DEV Community

Cover image for A python website  no framework  needed part 1
Abderrahmane Mustapha
Abderrahmane Mustapha

Posted on • Updated on

A python website no framework needed part 1

Why would you want to build a website using python with no framework here is some reasons :

  1. you want to understand how this flask , django works
  2. want to build your own framework in the future
  3. you like to build things from scratch

writing a simple python website without a framework is easy if you just want some basics features,
but if you want more advanced features like routing, database connection, validate forms, and add your own template engine then things will become difficult bit by bit

Requirements

requirements or what you need to know before you can start doing this

  1. a basic knowledge about http protocol, this is some resources if you want to learn more about how http work :

    MDN Basics of HTTP

    W3School HTTP Request Methods

  2. what WSGI is? , some resources if you want to know :
    An Introduction to the Python Web Server Gateway Interface

    What is WSGI

  3. how to serve a python web app using something like gunicorn , gevent

A simple hello World

here a simple example of a hello web python application

def app(environ, start_fn):
    start_fn('200 OK', [('Content-Type', 'text/plain')])
    return ["Hello World!\n"]
Enter fullscreen mode Exit fullscreen mode

but how to run this ? , first we need to install gunicorn or gevent or something similar, for me i choose gevent because gunicorn will not work with windows.

Installing genvent

pip install gevent
Enter fullscreen mode Exit fullscreen mode

Running the app

we need to import the gevent wsgi class to serve the app

....

if __name__ == '__main__':
    from gevent.pywsgi import WSGIServer
    WSGIServer(('', 8000),app, spawn=None).serve_forever()
Enter fullscreen mode Exit fullscreen mode

want to know why we use

if __name__ == '__main__':
Enter fullscreen mode Exit fullscreen mode

check this stackoverflow answer by Jack

our code will look like this now

def app(environ, start_response):
        data = b"Hello, Web!\n"
        start_response("200 OK", [
            ("Content-Type", "text/plain"),
            ("Content-Length", str(len(data)))
        ])
        return iter([data])


if __name__ == '__main__':
    from gevent.pywsgi import WSGIServer
    WSGIServer(('', 8000),app, spawn=None).serve_forever()
Enter fullscreen mode Exit fullscreen mode

now you can go to your terminal and run

python app.py
Enter fullscreen mode Exit fullscreen mode

check localhost:8000 and you will see a hello web message in your favorite web browser

in the next part, we will write a code to render an html file, create multiple pages, and navigate between them.

For Further Exploration

How to write a Python web framework

Python Web Applications: The basics of WSGI

How to use Flask with gevent (uWSGI and Gunicorn editions)

The u wsgi project

Simple Python Framework from Scratch

Social footer

By Toumi abderrahmane twitter . github

Top comments (0)