Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
External Middleware
Internal Middleware
Calling WSGI Applications
Frequently Asked Questions
What purposes serve web2py?
Describe TinyDB.
What is WSGI?
Does Web2py outperform Django?
Does Python 3 support web2py?
Last Updated: Mar 27, 2024

WSGI In Web2py

Author Mayank Goyal
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


WSGI and web2py coexist in a love-hate relationship. In our opinion, and this is why we use it, WSGI was created as a set of protocols to connect web servers to web applications in a portable fashion. At its core, web2py is a WSGI application, specifically gluon.main.wsgibase.

Lets learn

In this blog, we will learn WSGI In web2py in detail. So without any further ado, let's dive deeper into the topic. 


Some programmers have used WSGI as a middleware communication protocol beyond the breaking point, building web applications like an onion with multiple levels (each layer being a WSGI middleware developed independently of the entire framework). Internally, web2py does not use this structure. We believe that speed and security can be improved if a framework's essential functions—handling cookies, sessions, failures, transactions, and dispatching—are handled by a single comprehensive layer.


However, there are three ways (and their combinations) in which you can use middleware and WSGI apps from third parties using web2py: 

  • Any WSGI middleware from a third party can be added by editing the file ""
  • You can link any single app activity to third-party WSGI middleware.
  • A third-party WSGI program can be called from your activities.

The sole restriction is that you cannot swap out essential web2py routines with middleware from a third party.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

External Middleware

Consider the file "":  

    application = gluon.main.appfactory(wsgiapp=gluon.main.wsgibase,
    application = gluon.main.wsgibase

The middleware method gluon. factory wraps gluon. main.wsgibase when LOGGING is set to True. Logging is available to the "httpserver.log" file. You can include any third-party middleware similarly.

Internal Middleware

You can use a web2py decorator to apply middleware to any action in your controllers (for instance, index) and any third-party middleware application (for instance, MyMiddleware, which changes the output to upper case). Here's an illustration 👨‍💻:

class MyMiddleware:
    """converts output to upper case"""
    def __init__(self, app): = app
    def __call__(self, environ, start_response):
        items =, start_response)
        return [item.upper() for item in items]

def index():
    return 'hello world'

We cannot guarantee that this approach will function with all third-party middleware.

Calling WSGI Applications

A web2py action can easily call a WSGI application. Here's an illustration👨‍💻:

def test_wsgi_app(environ, start_response):
    """this is a test WSGI app"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', '13')]
    start_response(status, response_headers)
    return ['hello world!\n']

def index():
    """a test action that calls the previous app and escapes output"""
    items = test_wsgi_app(request.wsgi.environ,
    for item in items:
        response.write(item, escape=False)
    return response.body.getvalue()

In this instance, the test WSGI app is called by the index action, and the resulting value is copied verbatim to the page output. Remember that the index is not a standalone WSGI application and must use the standard web2py API (such as response. write to write to the socket).

Frequently Asked Questions

What purposes serve web2py?

Python dynamic web content programming is made possible via Web2py. Web2py is made to make laborious web development jobs easier, such as creating web forms from scratch, while a web developer can still do it if necessary.

Describe TinyDB.

A non-visible part of an app that saves data is called TinyDB. Every time an App Inventor-made app is launched, it is initialized. This means that the value of a variable set by an app will not be remembered the next time the app is executed if the user changes the value of the variable and then quits the app.

What is WSGI?

Web Server Gateway Interface is the name of it. WSGI was the foundation upon which web2py was developed, and it offers methods for accessing alternate interfaces without WSGI. Graham Dumpleton's mod WSGI module, which Apache uses, supports WSGI. To connect to WSGI, web2py supplies a file called

Does Web2py outperform Django?

Due to its smaller size, simpler learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a significantly clearer syntax. This makes applications easier to comprehend, maintain, and create.

Does Python 3 support web2py?

On Python 2.7 and Python 3, web2py functions with CPython (the C implementation) and PyPy (Python written in Python).-


In this article, we study WSGI and external and internal middleware, and at last, we learned about calling WSGI applications.

To learn more about web2py, you can refer to Introduction to an image blogInstallation and startup in web2py, and Use of web2py

That's the end of the article. I hope you all like this article. 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Previous article
Third Party Modules In Web2py
Next article
What are HTML Helpers in web2py
Live masterclass