Sunday 13 December 2015

Executing python script from Web Browser without using Apache.


 In one of our last blog we have demonstrated that how to execute python script on web browser using Apache Web
Server. In this tutorial we will you how to execute python script in browser without using apache web server.. Python has one of it's own library CGIHTTPServer.py which creates a web server capable of performing as web server. For creating this web server all you need is python application version 2.7. 
Download link of python 2.7
1. For 32bit version 
2. For 64 bit version.

After downloading and installing, goto the python root directory.(i.e. c:\python27)
You have to run this command here.

C:\Python27>python -m CGIHTTPServer 

When you run this command it will start the server at default port 8000.
If you want to run it in a different port just add a port number after the command given above

C:\Python27>python -m CGIHTTPServer 9000

Now check whether you have a folder named cgi-bin on the python root directory.If not create one
using md or mkdir cgi-bin and changed to cgi-bin directory.
Put all your executable python script on this folder.

Now go to the browser and type localhost:9000/cgi-bin/sample.py

Though the server will run by typing localhost:9000 but python code will  only parsed by browser when executing script from cgi-bin folder.

Example sample.py
#D!\Python27\python

print "Content-type: text/html\n"


print "<html><head><title>Hello CGI</title></head>"

print "<body><h2>Hello from CGIHTTPServer<h2>r</body></html>"

Executing this script will show

Hello from CGIHTTPServer

No comments: