Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSGI protocol #2

Open
igormcsouza opened this issue Mar 7, 2024 · 2 comments
Open

WSGI protocol #2

igormcsouza opened this issue Mar 7, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@igormcsouza
Copy link
Owner

Describe how python implements PEP 333 (the wisg protocol) for http comunication

@igormcsouza igormcsouza self-assigned this Mar 7, 2024
@igormcsouza igormcsouza added the documentation Improvements or additions to documentation label Mar 7, 2024
@igormcsouza
Copy link
Owner Author

How does it work?

Well, python has a protocol standart on conversations between Web Applciations and App Servers which is called WSGI which stands for Web Server Gateway Interface. This was introduced on PEP 333 that explains how this communication would work. At the very low level the communication works like this:

def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

The entrypoint of the Framework will be a function that will get a environ variable with information on the request and a callback function which is the start_response that the App Server will use to deal with the response that is comming in. The framework have to deal with the headers, status code and response.

There is another better way to implement it, still with pure python, using the BaseHTTPRequestHandler which is a wrapper for the function above. We can do more advance stuff like dealing with HTTP verbs and stuff. Really nice!

Well, this is not ideal because you must deal with everything, even the pathing to the static files, but with a simple project like this is easy to have fun doing those low-level stuff.

As App Server I'm using the socketserver library, also a standart for python! Which deals with port and sockets, I thought on working with it too, but I'm too lazy now. Maybe if I have too much fun here I can think on writing my own App Server. But, this guy is also no ideal for production, we have uWSGI and Gunicorn that are way better and heavy-tested.

@igormcsouza
Copy link
Owner Author

Take a look at the project https://github.com/igormcsouza/pure-todo

@igormcsouza igormcsouza removed their assignment Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant