知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

Apache下运行Python WEB Applications的三种方式

发布时间:2014-09-05 16:34:44作者:知识屋

1, 传统CGI:
vim /var/www/cgi-bin/hello.py
 
 
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
print "Content-type: text/html/n" 
print "Hello World" 
chmod a+x hello.py
 
vim /etc/httpd/conf/httpd.conf
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
 
2, Mod_Python (http://www.modpython.org/)
 
wget http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.1.tgz
 
./configure --with-apxs=/data/apache2221/bin/apxs --with-python=/usr/bin/python
make
make install
vim /etc/httpd/conf.d/python.conf
LoadModule python_module modules/mod_python.so
 
<Directory /var/www/mod-python>
    AddHandler mod_python .py
    # mod_python.publisher
    PythonHandler mod_python.publisher
 
    PythonDebug On
</Directory>
 
vim /var/www/mod-python/mod_python_publisher.py
 
 
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
from mod_python import apache 
 
def index(req): 
    req.log_error('handler') 
    req.content_type = 'text/html' 
    req.send_http_header() 
    req.write('<html><head><title>Testing mod_python</title></head><body>') 
    req.write('Hello World! - mod_python.publisher') 
    req.write('</body></html>') 
<Directory /var/www/mod-python>
    AddHandler mod_python .py
    # custom handler
    PythonHandler mod_python_handler
 
    PythonDebug On
</Directory>
 
vim /var/www/mod-python/mod_python_handler.py
 
 
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
from mod_python import apache 
 
def handler(req): 
    req.log_error('handler') 
    req.content_type = 'text/html' 
    req.send_http_header() 
    req.write('<html><head><title>Testing mod_python</title></head><body>') 
    req.write('Hello World! - custom handler') 
    req.write('</body></html>') 
    return apache.OK 
3, Mod_wsgi (http://code.google.com/p/modwsgi/)
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
 
./configure --with-apxs=/data/apache2221/bin/apxs --with-python=/usr/bin/python
make
make install
 
vim /data/apache2221/conf/httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so
 
WSGIScriptAlias /wsgi/ /var/www/wsgi/
<Directory "/var/www/wsgi">
    Order allow,deny
    Allow from all
</Directory>
vim /var/www/wsgi/hello.py
 
 
#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
def application(environ, start_response): 
    status = '200 OK' 
    content_type = 'text/html' 
 
    output = ['Hello World'] 
 
    response_headers = [('Content-type', content_type)] 
    start_response(status, response_headers) 
    return output  
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜