Do-it-yourself Python framework: 3. The most streamlined Hello World

For the teaching code, let’s make some simplifications, the server address is always fixed, there are not so many complicated parameters, and the code is simplified and simplified.

First of all, we implement a basic shelf. The browser enters http://localhost:8080, and the web page outputs Hello World.

Create a subdirectory xweb, and create the following files according to the structure of web.py:

  • application.py
  • db.py
  • debugerror.py
  • form.py
  • http.py
  • httpserver.py
  • net.py
  • py3helpers.py
  • session.py
  • template.py
  • test.py
  • utils.py
  • webapi.py
  • wsgi.py
  • __init__.py

httpserver.py

We don't need to think about efficiency, just use the WSGI server provided by cheroot.

server = None
    
def run_simple(func, server_address=('0.0.0.0', 8080)):
    global server
    # No middleware to handle static resouce, do log...
    server = WSGIServer(server_address, func)

    try:
        server.start()
    except (KeyboardInterrupt, SystemExit):
        server.stop()
        server = None

def WSGIServer(server_address, wsgi_app):
    from cheroot import wsgi
    
    return wsgi.Server(server_address, wsgi_app, server_name='localhost')

Access to static resources is not considered here, and it will be implemented slowly later. HTTPS access is also not considered. Just call run_simple to start the service.

wsgi.py

Regardless of various parameter configurations, FCGI, FastCGI, and SCGI are not considered at all.

from . import httpserver

def runwsgi(func):
    return httpserver.run_simple(func)

application.py

Path mappings are ignored and just return Hello World no matter what. wsgifunc returns a handler function for wsgi.runwsgi to call.

from . import wsgi

__all__ = ['application'] # Limit what'll be exported

class application:
    def __init__(self, mapping={}):
        pass

    def wsgifunc(self, *middleware):
        def wsgi(env, start_response):
            result = b'Hello World' # Fix the return for demo
            start_response('200 OK', ())
            return [result]

        # Ignore middleware now
        return wsgi

    def run(self, *middleware):
        return wsgi.runwsgi(self.wsgifunc(*middleware))

Well, our basic shelf has been successfully completed. Wait, it's actually a small step away, there are still some things to fill in __init__.py.

__init__.py

The purpose of this file is to identify that the directory is a python module package. When the user code executes import xweb, the __init__.py file in the xweb directory will be executed first. Here we first add a simple line:

from .application import *

Otherwise, if we want to use the application class in the code, we must first import xweb.application, and then app = application.application(...).

Hello World

Well, now we write the most streamlined Hello World Hello World application.

import sys
sys.path.append('.')

import xweb

app = xweb.application()
        
if __name__ == '__main__':
    app.run()

After execution, enter http://localhost:8080 in the browser address bar to see what the page returns.

You can directly download the source code of this chapter, and execute python demo.py after decompression.

Guess you like

Origin blog.csdn.net/panda_lin/article/details/121676153