Write Simple CGI Server In Python

Hello Friends,



                       Welcome Again To My Blog. Friends, Today, I Am Going To Write About CGI Module. So, Readers Keep Reading This Post Carefully.

Let'S Start.

After Reading Title CGI, The First Question Comes In Our Mind,

Q. What Is A CGI (Common Gateway Interface) ?.

Ans. As Wikipedia: The Simplest CGI Script That Can Be Considered Interesting Involves Printing Out An Http Header ("Content-Type: Text/Html") And A Web Page. In Addition, You Might Want To Handle Any Incoming Inputs From Things Like Html Forms Or Request Parameters. In The Earliest Days Of CGI, Shell Scripts Were Sometimes Used To Do Things Like This, So The Principles Are Not Particularly Advanced.

In Simple Words, CGI Script Is One Type Of Programmable Web Page Where You can perform all Web Functionalities, HTTP request/response and Other Operations Using Any Programming Language. I Hope You All Are Already Aware About How A Html Page Works.

For Example, Whenever We Do Any Change In Html Web Document, We Can Easily Check Those Changes In A Browser. Same Scenario Comes With CGI Script. CGI Script, Is One Of Web Html Generating Script Written In Any Scripting Language Like Python, Perl, Ruby Etc That Manipulate Html Document According To Programmer Designed Codes Condition.

Configuration

It can be an annoying experience getting some type of permissions just right on the script so that Web servers like Apache will run it, but the following checklist may be of some use:
  1. Find out which user runs the Web server - it's not often the same one as your own user, and it may be one with very limited permissions.
  2. Check the server configuration to see if it lets you run scripts in a particular directory. Make sure that if you're using a configuration file for a particular directory, the global configuration permits you to define CGI script directories in that directory-local configuration file - some sites stop their users from altering such settings in such a way.
  3. Check the permissions from the top of the filesystem down to the directory where the script resides. The Web server user must be able to read and open/execute all the directories from the top right down to the script.
  4. Make sure your script is readable and executable by the Web server user.
  5. Make sure that the first line of the script refers to an interpreter that the Web server user can run. Things like /usr/bin/env python might not have any meaning to the Web server user because the python program may not be on the user's PATH.
 So, friends let's start quickly

Now, First Step is to Create an Empty Folder and named that Folder anything as your need.  here I named example1. Now Create another folder under example1 and named that folder only "cgi-bin" because we will copy all  CGI Script here for Working Correctly.

here, I am going to show you how you can create a simple web page.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/python

import cgi
import cgitb; cgitb.enable()

#+++++++ Html Codings ++++++++++++++++++++++
print "Content-type:text/html\r\n\r\n\r\n"
print """
<html>
<title>Welcome To Hack World With SSB index Page</title>
<body>
<h1>Hello! World</h1>
</body></html>"""

now, save this code as hello.py on /example1/cgi-bin/hello.py

Before running your CGI program, make sure you have change mode of the file using chmod 755 hello.py UNIX command to make file executable.

After This We Need a Server To Host This Script. So, You Can Use any CGI Supporting Server. But Here I am using python in-built Server.

Copy all The codes Given Below And Save Them As on /example1/server.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env python2

import CGIHTTPServer
import BaseHTTPServer

if __name__ == "__main__":
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("", 8000)
    # Note that only /cgi-bin will work:
    handler.cgi_directories = ["/cgi-bin", "/cgi-bin/subdir"]
    httpd = server(server_address, handler)
    httpd.serve_forever()

Then, Execute This Script.

During The Execution of server.py  Open Browser and Type
Url Address : 127.0.0.1:8000/cgi-bin/hello.py
And Boom! You Got The Webpage.

Now, Let me Example you How This CGI Script works?
Basically, To make a CGI Script in Python, We have to use 2 modules, CGIHTTPServer and BaseHTTPServer. CGIHTTPServer Script is Handle CGI code available in Python CGI Script. In Simple words, This module handles Internal CGI operations. And BaseHTTPServer, handle HTTP request/response between Browser and Server Script.


import CGIHTTPServer
import BaseHTTPServer


So, In This Part First Create BaseHTTPServer Object To handle all Browser and Server related Communications.
and Then, Create Another Object To Handle CGI Operations

if __name__ == "__main__":
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("", 8000)


In this Last, apply all objects, provide function and deploy it. Very Simple!


import CGIHTTPServer
import BaseHTTPServer

if __name__ == "__main__":
    server = BaseHTTPServer.HTTPServer
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    server_address = ("", 8000)
    # Note that only /cgi-bin will work:
    handler.cgi_directories = ["/cgi-bin", "/cgi-bin/subdir"]
    httpd = server(server_address, handler)
    httpd.serve_forever()


Share this

Related Posts

Previous
Next Post »