search
HomeBackend DevelopmentPython TutorialSummary of 9 deployment methods of python web programs

Python has many web development frameworks. After the code is written, deployment and launch is a big deal. Generally speaking, web applications are generally three-tier structure web server ---->application -----> DB server

You can count the mainstream web servers with one slap, apache, lighttpd, nginx, iis

application, the Chinese name is called application service, which is the application code you write based on a certain web framework DB server general Refers to storage service. MySQL is mostly used in web development. In recent years, due to the expansion of website scale, key-value storage such as memcache and redis has also become popular.
The web server placed at the front has 3 functions

Highly efficient processing of static files, the web server is developed in C, and the calls are native functions, with targeted optimization of IO and file transfer

Acts as a simple network firewall, you can denny Some IPs, simply controlling the number of concurrent connections, etc. are better than nothing

Handling high concurrent short connection requests, forwarding requests from thousands of users through dozens of long connections on the intranet, one reason is The web server is very professional in handling high concurrency. Another reason is that the frameworks used by most applications do not have the ability to handle high concurrency

In fact, some web frameworks on the market have built-in support for epoll/kqueue, etc. Efficient network libraries have the ability to handle high concurrency, such as Python's tornado, Java-based tomcat, jetty, etc. Some people remove the front-end web server and run naked. However, when deploying public network applications, it is best not to In this way, because of the two reasons mentioned above 1 and 2, the network conditions from the user browser to the web server are all kinds of strange, which you can't imagine.

Web server strongly recommends using nginx for three reasons

Excellent performance, very stable
Easy to install, few dependent packages
The conf file is very easy to configure, simpler than apache/lighttpd
There are 9 ways to deploy web programs developed in python

mod_python, this is a built-in module of apache. It depends heavily on the python version mod_python is compiled and used with. It is not recommended for use with apache.

cgi is too old and not recommended, and nginx does not support cgi. Method, you can only use lighttpd or apache

fastcgi. This is the most popular method at present. It is supported by the flup module. The corresponding configuration instruction in nginx is fastcgi_pass

spawn-fcgi , this is the fastcgi multi-process management program, which comes with the lighttpd installation package. It has the same effect as flup. The difference is that flup is introduced at the python code level, and spawn-fcgi is an external program. spawn-fcgi is very versatile and can support code developed in any language, such as php, python, and perl. As long as your code implements the fastcgi interface, it can help you manage your process.

scgi, the full name is Simple Common Gateway Interface is also an alternative version of cgi. The scgi protocol is very simple. I think it is similar to fastcgi, but it has not been widely promoted. The corresponding configuration command of nginx is scgi_pass. You can use it if you want. Flup also supports it.

http, nginx uses proxy_pass forwarding. This requires that the back-end application must have a built-in http server that can handle high concurrency. In the python web framework, you can only choose tornado.

Python programmers I like to invent the wheel. In addition to being a web framework, tornado can also provide a high-performance http server independently. Therefore, if you use other python frameworks to write code, such as bottle, you can also start a high-performance one by importing tornado. http server can also be deployed using http protocol and nginx. Expanding, there are many http servers in the python package that can handle high concurrency, such as gevent, which can also be referenced by other frameworks to support http deployment.

In reality, when using java to make web programs, http and nginx are usually used. The application server chooses tomcat or jetty

uwsgi, which consists of 4 parts,

uwsgi Protocol
Web server built-in support protocol module
application server protocol support module
Process control program

nginx has built-in support for the uwsgi protocol starting from 0.8.4. The uwsgi protocol is very simple, with 4 words each Section header is a body. The body can be packages of many protocols, such as http, cgi, etc. (marked by fields in the header). I once did a small-scale performance comparison test. The results showed that compared with fastcgi, uwsgi has no performance. The obvious advantage may also be due to the smaller data set

uwsgi is characterized by its own process control program. It is written in C language and uses the natvie function. In fact, it is the same as spawn-fcgi/php- fpm is similar. Therefore, uwsgi can support a variety of application frameworks, including (python, lua, ruby, erlang, go), etc.

Gunicorn, a tool similar to uwsgi, is transplanted from the rails deployment tool (Unicorn). But the protocol it uses is WSGI, the full name is Python Web Server Gateway Interface, which is the official standard (PEP 333) defined in python2.5. It has good roots and is relatively simple to deploy. http://gunicorn.org/ There is a detailed tutorial

mod_wsgi, a module of Apache that also supports the WSGI protocol, https://code.google.com/p/modwsgi/

fastcgi protocol and http protocol are used in code deployment Comparison of the advantages and disadvantages

Although fastcgi is a binary protocol, it does not save resources compared to the http protocol. The binary protocol can only save the expression of numbers, such as 1234567. It takes 7 Bytes to express it as a string, and 4 Bytes as a number, and the string is the same everywhere

When fastcgi transmits data, in order It is compatible with the cgi protocol and also brings a bunch of cgi environment variables. Therefore, compared with the http protocol, using fastcgi to transmit data is not economical, but more efficient.

The only advantage of fastcgi is that it is a long-term connection. , if the user makes 1000 concurrent requests, fastcgi may use 10 links to forward them to the back-end application. If you use the http protocol, you can give as many as you want, and 1000 requests will be initiated to the back-end application

http proxy forwarding method , problems will arise when facing ultra-high concurrency, because in the tcp protocol stack, port is an int16 integer. When you create a new connect locally, you need to consume a port, which can be up to 65536. There are hundreds of thousands of external concurrent requests, the port pool is exhausted, and your server can only refuse to respond.

Summary

My personal habit is to use the fastcgi protocol to deploy python programs. It is simple and trouble-free. Choose technology Solution, be sure to choose the simplest and most common one. The fastcgi running script of this blog is as follows

kill - `cat / tmp / django.pid`
echo 'restart django....' 
python . / manage.py runfcgi - - settings = lutaf.settings_r maxchildren =  maxspare = minspare =  method = prefork pidfile = / tmp / django.pid host = 127.0 . 0.1  port = outlog = / tmp / dj.out errlog = / tmp / dj.error

It is recommended that everyone try Gunicorn. This is the future development direction

The above is the detailed content of Summary of 9 deployment methods of python web programs. 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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment