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 applicationAfter 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!
The Main Purpose of Python: Flexibility and Ease of UseApr 17, 2025 am 12:14 AMPython's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.
Python: The Power of Versatile ProgrammingApr 17, 2025 am 12:09 AMPython is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.
Learning Python in 2 Hours a Day: A Practical GuideApr 17, 2025 am 12:05 AMYes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.
Python vs. C : Pros and Cons for DevelopersApr 17, 2025 am 12:04 AMPython is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.
Python: Time Commitment and Learning PaceApr 17, 2025 am 12:03 AMThe time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.
Python: Automation, Scripting, and Task ManagementApr 16, 2025 am 12:14 AMPython excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
Python and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AMTo maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.
Python: Games, GUIs, and MoreApr 13, 2025 am 12:14 AMPython excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft







