Print all CGI environment vairables in Python

Discussion in 'Python' started by pradeep, Nov 27, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Earlier today, I had posted a code snippet to print all the CGI environment variables in CGI-Perl. Well, I recently started learning Python too, so thought of doing the same in Python. Checkout the code below.

    Code:
    #!/usr/bin/python
     import sys, os
     
     print "Content-Type: text/html\n\n";
     
     os.environ["TEST_VAR"] = "go4expert.com"
     for name, value in os.environ.items():
             print "%s\t= %s <br/>" % (name, value)
    Output:
    Code:
    SERVER_SOFTWARE	= Apache/2.0.52 (Red Hat) 
      SCRIPT_NAME	= /~a0600/cgi-bin/test.py 
      SERVER_SIGNATURE	= Apache/2.0.52 (Red Hat) Server at 192.168.0.166 Port 80  
      REQUEST_METHOD	= GET 
      HTTP_KEEP_ALIVE	= 300 
      TEST_VAR	= go4expert.com 
      SERVER_PROTOCOL	= HTTP/1.1 
      QUERY_STRING	=  
      PATH	= /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin 
      HTTP_ACCEPT_CHARSET	= ISO-8859-1,utf-8;q=0.7,*;q=0.7 
      HTTP_USER_AGENT	= Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0 
      HTTP_CONNECTION	= keep-alive 
      SERVER_NAME	= 192.168.0.166 
      REMOTE_ADDR	= 192.168.3.95 
      SERVER_PORT	= 80 
      SERVER_ADDR	= 192.168.0.166 
      DOCUMENT_ROOT	= /share/html 
      SCRIPT_FILENAME	= /share/a0600/public_html/cgi-bin/test.py 
      SERVER_ADMIN	= root@localhost 
      HTTP_HOST	= 192.168.0.166 
      HTTP_CACHE_CONTROL	= max-age=0 
      REQUEST_URI	= /~a0600/cgi-bin/test.py 
      HTTP_ACCEPT	= text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 
      GATEWAY_INTERFACE	= CGI/1.1 
      REMOTE_PORT	= 4648 
      HTTP_ACCEPT_LANGUAGE	= en-us,en;q=0.5 
      HTTP_ACCEPT_ENCODING	= gzip,deflate 
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice