Three methods for running Python WEB Applications in Apache

Source: Internet
Author: User

1. Traditional 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 ('Req. write ('Hello World! -Mod_python.publisher ')
Req. write ('</body> <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 ('Req. write ('Hello World! -Custom handler ')
Req. write ('</body> 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 = '100 OK'
Content_type = 'text/html'
 
Output = ['Hello world']
 
Response_headers = [('content-type', content_type)]
Start_response (status, response_headers)
Return output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.