Python (programming language)
You can get Python from the following: http://python.org/download/. Simply download the Python installer and follow the instructions. Make sure to remember the directory you used to install Python. You will need this information at the top of each of your Python cgi scripts.

Running Python

Running python in IDE
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "hello"
hello
>>>
Running Python in Command line
If the python command, instead of displaying the interpreter prompt >>>, gives you a message like:
then you need to make sure that your computer knows where to find the Python interpreter. To do this you will have to modify a setting called PATH, which is a list of directories where Windows will look for programs.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command
Environment variable will probably tell you where it is installed;
the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk ... use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the
command
Check Python in command line


http://docs.python.org/faq/windows.html
Running a python program in command line
We have a python program hello.py in C:\
The program prints Hello world
Running in command line

Configuring the Apache server (edit the httpd.conf )
Edit the httpd.conf (
Running the python web program in apache
In root folder in apache (www) we have created one folder for python programs
And we have created hello.py in side C:\AppServ\www\python
Program looks like this
Then point your browser to:
http://localhost/python/hello.py
Result in browser
Another Simple HTML page written in Python
#!C:\Python27\python.exe
print 'Content-Type:text/html\n\n'
print '<HTML><HEAD>'
print '<TITLE>Hello World - first Python Program</TITLE></HEAD>'
print '<BODY><H2>Hello World </H2>'
print '<P>Program with'
print '<A HREF="http://www.python.org/"'
print '>Python</A> today</BODY></HTML>'
Save this code with hello_world.py and place this file in C:\AppServ\www\python
and view in browsers http://localhost/python/hello_world.py
http://www.editrocket.com/articles/python_apache_windows.html
http://my.opera.com/NoteMe/blog/running-python-as-cgi-in-apache-in-windows
CGI Architecture Diagram
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Its syntax is said to be clear and expressive. Python has a large and comprehensive standard library.
Python supports multiple programming paradigms, primarily but not limited to object-oriented, imperative and, to a lesser extent, functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems.
Install Python
You can get Python from the following: http://python.org/download/. Simply download the Python installer and follow the instructions. Make sure to remember the directory you used to install Python. You will need this information at the top of each of your Python cgi scripts.
Running Python
Running python in IDE
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "hello"
hello
>>>
Running Python in Command line
If the python command, instead of displaying the interpreter prompt >>>, gives you a message like:
then you need to make sure that your computer knows where to find the Python interpreter. To do this you will have to modify a setting called PATH, which is a list of directories where Windows will look for programs.
You should arrange for Python’s installation directory to be added to the PATH of every command window as it starts. If you installed Python fairly recently then the command
Environment variable will probably tell you where it is installed;
the usual location is something like C:\Python27. Otherwise you will be reduced to a search of your whole disk ... use Tools ‣ Find or hit the Search button and look for “python.exe”. Supposing you discover that Python is installed in the C:\Python27 directory (the default at the time of writing), you should make sure that entering the
command
Check Python in command line
http://docs.python.org/faq/windows.html
Running a python program in command line
We have a python program hello.py in C:\
The program prints Hello world
Running in command line
2. Configure Apache to run Python CGI (before you start you shoud have a apache web server )
install Apache web server - http://umeshnarayanan1983.blogspot.in/2012/04/what-is-php-php-stands-for-p-hp-h.html , http://www.apache.org/
Installing Apache
Configuring the Apache server (edit the httpd.conf )
Edit the httpd.conf (
- And locate the line that looks like: #ScriptInterpreterSource Registry
Remove
the comment (#) , and there you go. Now windows will use the registry
to find out where Python is installed, and you have all read read past
the part where I asked you to check if you could run Python script, so
that's really it. (i have installed Appserve - so this http.config - will be in C:\AppServ\Apache2.2\conf)
ScriptInterpreterSource Registry
- there is still a missing link between your Python scripts and Apache. You need to tell Apache what file types you are using for Python. The most usual file type is .py, but anything goes. But using .py is a good start. So scroll down on to the bottom of your httpd.conf file, and add the line:
AddHandler cgi-script .py .cgi
- There might be a few other words added to it, or less, but it should be between the <Directory> tags pointing to your root folder. At the end add ExecCGI, which basically permits Apache to execute CGI scripts, and if you did write it in your root <Directory>, it will automatically give Apache access to run it in all sub directories as well. This is both great freedom for you, but also for hackers if they find holes in your scripts they are able to take advantage of. So be warned. For this approach, FastCGI might be a safer bet, but again you have the problem with it not being installed on too many servers around the world. But maybe I will find the time to write up a little something about that later on.
Add the following lines
<Directory "C:\AppServ\www\python">
Options ExecCGI
Order allow,deny
Allow from all
SetHandler cgi-script
</Directory>
Directory where you have written your python scriptsRunning the python web program in apache
In root folder in apache (www) we have created one folder for python programs
And we have created hello.py in side C:\AppServ\www\python
Program looks like this
Then point your browser to:
http://localhost/python/hello.py
Result in browser
Another Simple HTML page written in Python
#!C:\Python27\python.exe
print 'Content-Type:text/html\n\n'
print '<HTML><HEAD>'
print '<TITLE>Hello World - first Python Program</TITLE></HEAD>'
print '<BODY><H2>Hello World </H2>'
print '<P>Program with'
print '<A HREF="http://www.python.org/"'
print '>Python</A> today</BODY></HTML>'
Save this code with hello_world.py and place this file in C:\AppServ\www\python
and view in browsers http://localhost/python/hello_world.py
Reference:-
http://ujjaini.wordpress.com/2010/08/13/how-to-run-python-script-on-apache-on-windows/http://www.editrocket.com/articles/python_apache_windows.html
http://my.opera.com/NoteMe/blog/running-python-as-cgi-in-apache-in-windows
What is CGI ?
- The Common Gateway Interface, or CGI, is a set of standards that define how information is exchanged between the web server and a custom script.
- The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows:
- The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers.
- The current version is CGI/1.1 and CGI/1.2 is under progress.
Web Browsing
To understand the concept of CGI, lets see what happens when we click a hyper link to browse a particular web page or URL.- Your browser contacts the HTTP web server and demand for the URL ie. filename.
- Web Server will parse the URL and will look for the filename in if it finds that file then sends back to the browser otherwise sends an error message indicating that you have requested a wrong file.
- Web browser takes response from web server and displays either the received file or error message.
However,
it is possible to set up the HTTP server so that whenever a file in a
certain directory is requested that file is not sent back; instead it is
executed as a program, and whatever that program outputs is sent back
for your browser to display. This function is called the Common Gateway
Interface or CGI and the programs are called CGI scripts. These CGI
programs can be a Python Script, PERL Script, Shell Script, C or C++
program etc.
No comments:
Post a Comment