search
HomeBackend DevelopmentPython TutorialBottle source code reading notes (1): WSGI

Bottle source code reading notes (1): WSGI

Jun 23, 2017 pm 03:06 PM
bottleSource codenotesread

Preface

Bottle is a Python web framework. The entire framework has only one file, less than 4k lines of code, and no dependencies other than the Python standard library. However, it includes common functions of web frameworks such as routing, templates, and plug-ins. There is no better time than reading the Bottle source code to understand what a web framework is and how it works. Since Bottle is a framework that supports WSGI, before reading the source code, let's first understand what WSGI is.

Note: The Bottle version used in this article is 0.12.13.

WSGI

##General web servers can only handle static pages. If dynamic content is involved, the server needs to interact with server languages ​​such as Java/Python/Ruby and hand over the content to them for processing. Since most web servers are written in C, they cannot directly execute the server language, so a bridge is needed between the two (in practical applications, an application server is usually added between the web server and the WSGI application to support WSGI) . In Python, WSGI is such a bridge. The implementation of WSGI is divided into two parts, one is the server and the other is the application. Let’s take a look at what each of them looks like and how the two work together.

 1 class Server: 2  3     def __init__(self, server_address): 4         self.server_address = server_address 5  6     def set_app(self, application): 7         self.app = application 8  9     def serve_forever(self):10         while True:11             # socket.accept()12             if request_comein():13                 self.handle_request()14 15     def handle_request(self):16         request_data = self.get_request()17         self.parse_request(request_data)18         environ = self.get_environ()19         result = self.application(environ, self.start_response)20         self.send_response(result)21 22     def start_response(self, status, headers, exc_info):23         pass24 25     def get_environ(self):26         pass27 28     def get_request(self):29         pass30 31     def parse_request(self, text):32         pass33 34     def send_response(self, message):35         pass36 37 38 def make_server(host, port, app, server=Server):39     server = server((host, port))40     server.set_app(app)41     return server42 43 def simple_app(environ, start_response):44     status = '200 OK'45     response_headers = [('Content-type', 'text/plain')]46     start_response(status, response_headers)47     return 'Hello World!'48 49 if __name__ == '__main__':50     server = make_server('localhost', 8080, simple_app)51     server.serve_forever()

Due to space limitations, this server model omits many details. If you want a simple and operational WSGI server, you can refer to Let's Build A Web here Server.Part 2.

After receiving the request, the server parses the information in the request message and saves the result in a dictionary named environ. Then the application application (environ, start_response) is called with environ and the start_response function that processes header information as parameters. Finally, the results of the application are composed into a new response and sent back to the client.

On the application side, a WSGI application is a callable object. It can be a function, method, class, or an instance with a __call__ method. The above application is a function.

When various servers and applications/frameworks are developed in accordance with WSGI standards, we can freely combine different servers and frameworks according to our needs.

Bottle's simplest application

After briefly understanding WSGI, we return to Bottle to observe what a Bottle application looks like, how to run it, and how to follow our What is the difference between models.

1 from bottle import Bottle, run2 3 app = Bottle()4 5 @app.route('/hello')6 def hello():7     return 'Hello World!'8 9 run(app, host='localhost', port=8080, server='wsgiref')

Now run this program and use the browser to access the address 'localhost:8080/hello' and you will see 'Hello World!'.

#1. Unlike the above applications, the Bottle application is an instance. According to WSGI regulations, the Bottle object must implement the __call__ method:

1 def __call__(self, environ, start_response):2     ''' Each instance of :class:'Bottle' is a WSGI application. '''3     return self.wsgi(environ, start_response)

So this Bottle.wsgi method is the entrance for the server to call the Bottle application, and it is also the entrance for us to read Entry to the source code.

2. @app.route() This decorator binds a function to a URL. When we access 'localhost:8080/hello', the hello function will be called.

3. Bottle's default server is wsgiref (a simple implementation of WSGI in the Python standard library). Of course, Bottle has also written adapters for many servers. You only need to change the value of server, and the run() function will find the corresponding adapter based on the name of the server. No need to write additional code.

run function and adapter part code:

 1 def run(app=None, server='wsgiref', host='127.0.0.1', port=8080, 2         interval=1, reloader=False, quiet=False, plugins=None, 3         debug=None, **kargs): 4     if server in server_names: 5         server = server_names.get(server) 6     if isinstance(server, basestring): 7         server = load(server) 8     if isinstance(server, type): 9         server = server(host=host, port=port, **kargs)10     if not isinstance(server, ServerAdapter):11         raise ValueError("Unknown or unsupported server: %r" % server)12     ...13     server.run(app)14 15 class MeinheldServer(ServerAdapter):16     def run(self, handler):17         from meinheld import server18         server.listen((self.host, self.port))19         server.run(handler)
Finally

In this article, we A brief introduction to how servers and applications interact under the WSGI standard. In the next article, we will continue to focus on this simplest application and talk about the routing functions related to @app.route().

The above is the detailed content of Bottle source code reading notes (1): WSGI. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

What are unit tests in Python?What are unit tests in Python?Apr 30, 2025 pm 02:05 PM

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

What are Access Specifiers in Python?What are Access Specifiers in Python?Apr 30, 2025 pm 02:03 PM

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

What is __init__() in Python and how does self play a role in it?What is __init__() in Python and how does self play a role in it?Apr 30, 2025 pm 02:02 PM

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

What is the difference between @classmethod, @staticmethod and instance methods in Python?What is the difference between @classmethod, @staticmethod and instance methods in Python?Apr 30, 2025 pm 02:01 PM

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function