how to create a simple cgi server using python

Hello Friends,



                        Today, we will learn about CGI Script. so, first of all, what is CGI (Common Gateway Interface) Scripts.

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.

Configuration


It can be an annoying experience getting the permissions just right on a 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

Now, 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 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 For This. So, You Can Use any CGI Supporting Server. But Here I am using python in-built Server.
For This 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.
After This, Open Browser and Type
Url Address : 127.0.0.1:8000/cgi-bin/hello.py
if you click hello.py, then this produces the following output −


Now, You Can Create Any CGI Scripts as Here, I Show.

created by:
S.S.B
surajsinghbisht054@gmail.com

Share this

Related Posts

Previous
Next Post »