歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Apache下運行Python WEB Applications的三種方式

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  
Copyright © Linux教程網 All Rights Reserved