Skip to content

Setting up a Python-CGI

8. December 2013

This is just a simple description on how to setup a basic Python-CGI!
Make sure to have ‘Apache2’ and Python (min V2.7) installed and you can start!

Set up a new Apache2 confiuration and test it

  1. Create a folder for your CGI:
     mkdir /home/user/cgi-projects/
     mkdir /home/user/cgi-projects/mycgi/
     mkdir /home/user/cgi-projects/mycgi/html
  2. Add a index.html file there:

    echo '<b>Cool! The Apache2 configuration has been changed!</b>' > /home/user/cgi-projects/mycgi/html/index.html
  3. Make a copy of the Apache2 default http-configuration file:
    sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mycgi
  4. Open it in an editor (gedit in my case):
     sudo gedit /etc/apache2/sites-available/mycgi
  5. Change the default directory … from:
      <Directory /var/www/>

    …to…

     <Directory /home/user/cgi-projects/mycgi/html/>
     
  6. Define this file (mycgi) as apache2 httpd configuration file:
     sudo a2dissite default && sudo a2ensite mycgi
  7. Restart Apache2:
     sudo /etc/init.d/apache2 restart
  8. Test it on: http://localhost

When you can see now the text Cool! The Apache2 configuration has been changed! you have done good so far. If not…check if you have adapted everything correctly…escpecially replacing ‘user’ with your login!

Building a basic CGI from that setup

  1. Add a CGI-folder to the project:
     mkdir /home/user/cgi-projects/mycgi/cgi-bin
  2. Add a python-file in there and open/save it:
    gedit /home/user/cgi-projects/mycgi/cgi-bin/mycgi.py
     
  3. Add these lines to that file:
     #!/usr/bin/env python
     print "Content-type: text/html\n"
     print "Ha! It even works with a python file!"
  4. Define permissions of this file (to make it accessible for Apache2):
    chmod +x /home/user/cgi-projects/mycgi/cgi-bin/mycgi.py
  5. Open the http configuration file again and adapt the cgi-bin definition:
    sudo gedit /etc/apache2/sites-available/mycgi
  6. Change the default directory … from:
     <ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/>

    …to…

    <ScriptAlias /cgi-bin/ /home/user/cgi-projects/mycgi/cgi-bin/>
     
  7. Restart Apache2:
    sudo /etc/init.d/apache2 restart
  8. Test it on: http://localhost/cgi-bin/mycgi.py

That’s it! You’ve got a basic Python-CGI on your local machine!

Use the CGI to return a JSON-file

This is my basic aim on the cgi-scripting, so I will describe this finally!

  1. Make a new python-file (json.py):
     gedit /home/user/cgi-projects/mycgi/cgi-bin/json.py
  2. Add these line to the file:
    #!/usr/bin/env python
    import simplejson as json
    print "Content-type: application/json\n"
    data = { 'a':'A', 'b':[2, 4], 'c':3.0 }
    #This way...
    #print json.dumps(data)
    #..or this way
    print(json.JSONEncoder().encode(data))

    Following some descriptions in the web, I made the failure to forget the ‘\n’ at the end of the first printed line.
    Consequential error message: ‘Premature end of script headers: json.py’

  3. Remember to change permissions:
     chmod +x /home/user/cgi-projects/mycgi/cgi-bin/json.py
  4. Testing it on: http://localhost/cgi-bin/json.py

Request the JSON-file from your CGI

Finally:

  1. Change the data output of json.py to:
     data = {'elements':['Content1', 'Content2', 'Content3']}
  2. Change the index.html file of your Apache2 configuration to that:
    <!DOCTYPE html>
    <html>
    <head>
     <title>CGI-Test</title>
     <meta charset="utf-8" />
     <script src="http://d3js.org/d3.v3.min.js"></script>
     <style></style>
    </head>
    <body>
     <script>
     d3.json("http://localhost/cgi-bin/json.py", function(error, data){
       d3.select('body').selectAll('div').data(data.elements).enter()
         .append('div').text(function(d){return d})
     })
     </script>
    </body>
    </html>

With this JavaScript implementation, we call the CGI-script and use the returned JSON-file to build add some contents to the html-site!

Acknowledgement:

These sites helped me a lot:

So thank you for your knowledge sharing!!!

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: