- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python部署web开发程序的几种方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、fastcgi ,通过flup模块来支持,在nginx里对应的配置指令是 fastcgi_pass 。
2、http,nginx使用proxy_pass转发,这个要求后端appplication必须内置一个能处理高并发的http server,在python的web框架当中,只能选择tornado. 。
3、uwsgi,包括4部分组成:
nginx从0.8.4开始内置支持uwsgi协议,uwsgi协议非常简单,一个4个字节header+一个body,body可以是很多协议的包,比如说http,cgi等(通过header里面字段标示).
uwsgi的特点在于自带的进程控制程序.它是用c语言编写,使用natvie函数,其实和spawn-fcgi/php-fpm类似。所以uwsgi可以支持多种应用框架,包括(python,lua,ruby,erlang,go)等等 。
4、mod_python,这是apache内置的模块,很严重的依赖于mod_python编译使用的python版本,和apache配套使用,不推荐 。
5、cgi,这个太old,不推荐,而且nginx不支持cgi方式,只能用lighttpd或者apache 。
6、spawn-fcgi,这个是fastcgi多进程管理程序,lighttpd安装包附带的,和 flup效果一样,区别是flup是 python代码级引入,spawn-fcgi是外部程序。spawn-fcgi用途很广,可以支持任意语言开发的代码,php,python,perl,只要你代码实现了fastcgi接口,它都可以帮你管理你的进程 。
7、scgi,全名是Simple Common Gateway Interface,也是cgi的替代版本,scgi协议很简单,我觉得和fastcgi差不多,只是没有怎么推广开来,nginx对应的配置指令是scgi_pass,你想用就用,flup也支持.
8、Gunicorn,和uwsgi类似的工具,从rails的部署工具(Unicorn)移植过来的。但是它使用的协议是 WSGI,全称是Python Web Server Gateway Interface ,这是python2.5时定义的官方标准(PEP 333 ),根红苗正,而且部署比较简单,http://gunicorn.org/ 上有详细教程 。
9、mod_wsgi,apache的一个module,也是支持WSGI协议,https://code.google.com/p/modwsgi/ 。
uwsgi 。
安装uwsgi 。
pip install uwsgi 。
配置uwsgi 。
uwsgi 有多种配置可用:
1
2
3
4
|
1
,ini
2
,xml
3
,json
4
,yaml
|
配置示例 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$ cat etc
/
uwsgi.ini
[uwsgi]
socket
=
127.0
.
0.1
:
9005
chdir
=
/
Users
/
suoning
/
python_project
/
trunk
/
wsgi
-
file
=
main.py
processes
=
4
stats
=
127.0
.
0.1
:
9000
daemonize
=
/
tmp
/
uwsgiServer.log
pidfile
=
/
tmp
/
uwsgi.pid
vacuum
=
true
log
-
maxsize
=
50000000
disable
-
logging
=
true
callable
=
app
$
|
配置参数详解:
常用选项:
socket : 地址和端口号,例如:socket = 127.0.0.1:50000 。
processes : 开启的进程数量 。
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number of workers / processes) 。
chdir : 指定运行目录(chdir to specified directory before apps loading) 。
wsgi-file : 载入wsgi-file(load .wsgi file) 。
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address) 。
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads) 。
master : 允许主进程存在(enable master process) 。
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上.
log-maxsize :以固定的文件大小(单位KB),切割日志文件。 例如:log-maxsize = 50000000 就是50M一个日志文件.
pidfile : 指定pid文件的位置,记录主进程的pid号.
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets) 。
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0) 。
配置nginx 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
$ cat etc
/
nginx
/
servers
/
tongbupan.conf
server {
listen
80
;
server_name localhost;
location
/
{
include uwsgi_params;
uwsgi_pass
127.0
.
0.1
:
9005
;
}
location
/
webstatic
/
{
expires
7d
;
add_header Cache
-
Control public;
alias
/
Users
/
suoning
/
probject
/
python_project
/
webstatic
/
trunk
/
;
}
}
$
$ nginx
-
t
nginx: the configuration
file
/
usr
/
local
/
etc
/
nginx
/
nginx.conf syntax
is
ok
nginx: configuration
file
/
usr
/
local
/
etc
/
nginx
/
nginx.conf test
is
successful
$
$ nginx
-
s
reload
$
|
配置application 。
flask 示例 。
1
2
3
4
5
6
7
8
9
|
...
app
=
Flask(
'pan'
)
...
if
__name__
=
=
'__main__'
:
# app.run(host='0.0.0.0', port=5000)
app.run()
# 注意:变量app对应uwsgi配置文件uwsgi.ini中 callable = app
|
启动uwsgi 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
$
$ uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
[uWSGI] getting INI configuration
from
/
usr
/
local
/
etc
/
uwsgi.ini
$
$ ps
-
ef|grep uwsgi
11428
1
0
11
:
40
下午 ??
0
:
01.23
uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
11432
11428
0
11
:
40
下午 ??
0
:
00.00
uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
11433
11428
0
11
:
40
下午 ??
0
:
00.00
uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
11434
11428
0
11
:
40
下午 ??
0
:
00.00
uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
11435
11428
0
11
:
40
下午 ??
0
:
00.00
uwsgi
-
-
ini
/
usr
/
local
/
etc
/
uwsgi.ini
11440
69240
0
11
:
40
下午 ttys000
0
:
00.00
grep uwsgi
$
$ lsof
-
i tcp:
9000
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF NODE NAME
uwsgi
11428
suoning
28u
IPv4
0x5583e11534d24e73
0t0
TCP localhost:cslistener (LISTEN)
$
$ lsof
-
i tcp:
9005
COMMAND PID USER FD
TYPE
DEVICE SIZE
/
OFF NODE NAME
uwsgi
11428
suoning
6u
IPv4
0x5583e11535699e73
0t0
TCP localhost:
9005
(LISTEN)
uwsgi
11432
suoning
6u
IPv4
0x5583e11535699e73
0t0
TCP localhost:
9005
(LISTEN)
uwsgi
11433
suoning
6u
IPv4
0x5583e11535699e73
0t0
TCP localhost:
9005
(LISTEN)
uwsgi
11434
suoning
6u
IPv4
0x5583e11535699e73
0t0
TCP localhost:
9005
(LISTEN)
uwsgi
11435
suoning
6u
IPv4
0x5583e11535699e73
0t0
TCP localhost:
9005
(LISTEN)
$
|
FCGI 。
参考:http://webpy.org/cookbook/fastcgi-nginx 。
配置Nginx 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
$ cat etc
/
nginx
/
servers
/
pan.conf
server {
listen
80
;
server_name localhost;
error_page
500
502
503
504
/
50x
.html;
location
=
/
50x
.html {
root html;
}
location
/
{
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI
/
1.1
;
fastcgi_param SERVER_SOFTWARE nginx
/
$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass
127.0
.
0.1
:
9005
;
}
location
/
webstatic
/
{
expires
7d
;
add_header Cache
-
Control public;
alias
/
Users
/
suoning
/
probject
/
python_project
/
webstatic
/
trunk
/
;
}
}
$
|
配置application 。
简单示例 。
1
2
3
4
5
6
7
8
|
from
flup.server.fcgi
import
WSGIServer
from
pan
import
app
WSGIServer(
app,
bindAddress
=
(host, port),
maxThreads
=
threads
).run()
|
生产环境示例 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__
=
'suoning'
import
sys
import
argparse
from
flup.server.fcgi
import
WSGIServer
from
lib.daemon
import
Daemon
from
pan
import
app
APP_NAME
=
'pan_platform'
APP_INST_NAME
=
'20170501'
parser
=
argparse.ArgumentParser(description
=
u
'Run an pan FastCGI server'
)
parser.add_argument(
'command'
,
type
=
str
,
help
=
u
'command [start|stop|restart]'
,
choices
=
[
'start'
,
'stop'
,
'restart'
])
parser.add_argument(
'-p'
,
'--port'
,
type
=
int
,
help
=
u
'port of this server'
, required
=
True
)
parser.add_argument(
'-t'
,
'--threads'
,
type
=
int
, default
=
50
,
help
=
u
'max number of threads'
)
parser.add_argument(
'-host'
,
'--host'
, default
=
'0.0.0.0'
,
help
=
u
'Listen to the main clause'
)
class
panPlatformDaemon(Daemon):
def
run(
self
):
# 运行服务
try
:
WSGIServer(
app,
bindAddress
=
(args.host, args.port),
maxThreads
=
args.threads,
umask
=
0111
).run()
except
:
sys.stderr.write(
'oops'
)
def
gen_pidfile(port):
return
'/var/run/%s_%s_%d.pid'
%
(APP_NAME, APP_INST_NAME, port)
if
__name__
=
=
'__main__'
:
args
=
parser.parse_args()
daemon
=
panPlatformDaemon(gen_pidfile(args.port))
if
'start'
=
=
args.command:
daemon.start()
elif
'stop'
=
=
args.command:
daemon.stop()
elif
'restart'
=
=
args.command:
daemon.restart()
else
:
print
"Unknown command"
sys.exit(
2
)
sys.exit(
0
)
|
fastcgi协议和http协议在代码部署中的的优劣对比 。
CGI, FCGI, SCGI, WSGI 区别 。
WIKI Links
CGI - http://en.wikipedia.org/wiki/Common_Gateway_Interface FCGI - http://en.wikipedia.org/wiki/Fcgi SCGI - http://en.wikipedia.org/wiki/SCGI WSGI - http://en.wikipedia.org/wiki/Wsgi 。
Other reference
http://helpful.knobs-dials.com/index.php/CGI%2C_FastCGI%2C_SCGI%2C_WSGI%2C_servlets_and_such#FastCGI_and_SCGI 。
CGI = Common Gateway Interface 。
顾名思义,它是一种接口规范。该规范详细定义了Web服务器中运行的服务器代理程序,怎样获取及返回网页生成过程中,服务器环境上下文和HTTP协议中的参数名称,如大家所熟知的:REQUEST_METHOD,QUERY_STRING,CONTENT_TYPE等等。绝大部分的Web服务器程序,是以脚本的形式代理接受并处理HTTP请求,返回HTTP页面或响应。这些脚本程序,就是大家所熟知的PHP、ASP、JSP等等.
FCGI = Fast CGI 。
它其实是CGI在具体实现中的的一个变种。其设计思路是,通过减少CGI代理程序和Web宿主服务程序的通信开销,从而达到提高Web服务性能的最终目的。由此可见,FCGI在规范上跟CGI并没有不同,只是具体实现方式上有所改进:CGI的做法是,对于每个HTTP请求,Web宿主服务程序都建立新的进程以调用服务器脚本,相应该请求;FCGI的做法是,建立一个独立的FCGI服务程序进程,和Web宿主服务程序进程通信,FCGI服务进程被一旦启动后,自己分配资源、创建线程响应HTTP请求、并决定自身生命周期,从而大大降低了系统为了创建进程而做出的资源开销。现代流行的Web服务器程序,如PHP、ASP.Net,基本都是FCGI的实现.
SCGI = Simple CGI 。
它是FCGI在精简数据协议和响应过程后的产物。其设计目的是为了适应越来越多基于AJAX或REST的HTTP请求,而做出更快更简洁的应答。并且SCGI约定,当服务器返回对一个HTTP协议请求响应后,立刻关闭该HTTP连接。所以不难看出,SCGI更加适合于普遍意义上SOA所提倡的“请求-忘记”这种通信模式.
WSGI = Web Server Gateway Interface 。
此协议是Python语言的专利,它定义了一组在Web服务宿主程序和HTTP响应代理程序之间通信的普遍适用的接口。它的产生是因为Python程序员注意到,对于Web框架和Web宿主服务器程序间,有严重的耦合性,比如说,某些框架是针对Apache的mod_python设计的。于是,WSGI就定义了一套非常低级别的接口。常见的Python Web框架都实现了这个协议:如 CherryPy, Django, web.py, web2py, TurboGears, Tornado, Pylons, BlueBream, Google App Engine[dubious – discuss], Trac, Flask, Pyramid,等等. 。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我.
原文链接:http://www.cnblogs.com/suoning/p/6786621.html 。
最后此篇关于Python部署web开发程序的几种方法的文章就讲到这里了,如果你想了解更多关于Python部署web开发程序的几种方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!