avatarYang Zhou

Summary

The provided web content explains the concept of WSGI (Web Server Gateway Interface), its role in Python web development, and how it facilitates communication between Python web applications and server software.

Abstract

WSGI, as defined by PEP 3333, is a Python standard interface that simplifies web development by enabling a consistent way for Python web applications to interact with server software. The article outlines the typical process of a web request, from the browser sending an HTTP request to the server responding with an HTML document. It emphasizes the complexity of handling HTTP requests and responses, along with security concerns, which makes using a server software like Apache or Nginx a best practice. The WSGI interface serves as a bridge, allowing various Python web frameworks, such as Django and Flask, to communicate with different server software without needing to understand the specifics of each. The article also introduces the built-in wsgiref module in Python for development and testing, and recommends more robust WSGI implementations like Gunicorn or uWSGI for production environments. The author concludes by inviting readers to follow their work and consider using a cost-effective AI service for similar performance to ChatGPT Plus.

Opinions

  • The author suggests that without WSGI, Python web development would be chaotic due to the lack of standardized communication between apps and servers.
  • Using server software is presented as essential for handling the complexities of HTTP requests and responses, as well as security issues.
  • The wsgiref module is highlighted as a useful tool for developers to set up a simple server for testing purposes.
  • For production, the author recommends employing more efficient and robust WSGI server implementations such as Gunicorn or uWSGI.
  • The article promotes the author's Medium profile and a special offer for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus.

What is WSGI? A Readable Explanation for Python Developers

Understand the web server gateway interface completely

Photo by Steve Johnson on Unsplash

Introduction to the WSGI

When we enter a link on a browser, what will happen?

Simply put, there are only four steps:

  • The browser sends an HTTP request.
  • The web server receives the request and finds or generates an HTML document.
  • The server sends the HTML document as the body of a HTTP response to the browser.
  • The browser receives the HTTP response, renders and displays the HTML document in it.

Therefore, to develop a website in Python, we just need to implement these four steps. Sounds easy? 🙂

However, parsing HTTP requests, sending HTTP responses and handling all the relative issues are complex work. If we would like to write these programs by ourselves, we have to become an HTTP and network expert firstly. Not to mention there is much cyber security knowledge we have to know as well.

So, the best practice for web development is to use a server software, such as Apache or Nginx, to help us handle these common issues. And then we can focus on the business logic of our websites.

But as to websites written in Python, how can a server software communicate with Python programs?

If there are no common specifications or rules for this, Python web development will be messy. Problems like the following will emerge again and again:

  • How can Django apps communicate with Apache?
  • How can Flask apps communicate with Nginx?
  • ……
  • How can A (a Python web app/framework) communicate with B (a server software)?

Therefore, the Python community provides a standard interface by PEP 3333 to solve the problem.

This interface is called WSGI: Web Server Gateway Interface.

A software that implements this interface is called a WSGI server software.

By the way, just like there are a few different implementations of Python (Cpython, Jython, PyPy and so on). There also are different implementations of WSGI. Gunicorn and uWSGI are two of the most popular implementations.

Now, we got the whole picture. The backend architecture of a Python web app is like the following:

The backend architecture of a Python web app

Try the Built-in WSGI Server of Python

Python has a built-in module called wsgiref.

As its name implies, it’s a reference implementation of the WSGI standard without considering any efficiency. But it’s very useful for development and testing processes.

Let’s write a simple program to see how to use it:

After running the above code, we can open the corresponding address to see results:

The result after opening the address

We implemented a simple HTTP server with just a few lines of code. Again, it shows how elegant Python is.

We can check out more details about the wsgiref module from its official documents.

Conclusion

The WSGI represents for web server gateway interface. It’s a standard designed by the Python community to make the web development in Python easier. It builds a bridge between a Python web app and a server software.

For development and testing scenarios, we can use the built-in wsgiref module to setup a simple server in Python. For production environment, we need to use a more efficient and robuster WSGI implementation such as Gunicorn or uWSGI.

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles about programming and technologies!

Further reading:

Python
Programming
Web Development
Coding
Software Development
Recommended from ReadMedium