top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Configuraion to run pyhton script on ubuntu

+1 vote

I want to run a python script which contains simple form of html on firefox browser , but don't know what should be the configuration on ubuntu 12.04 to run this script i.e CGI configuration.

My code is

in /var/www/cgi-bin/

#!/usr/bin/env python
import webapp2

form ="""


class MainPage(webapp2.RequestHandler):
 def get(self):
 #self.response.headers['Content-Type'] = 'text/plain'

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
posted Jul 28, 2013 by Sonu Jindal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
Best answer

The first step is to update your server:

sudo apt-get update

Then install your Apache2 web server:

sudo apt-get install apache2

Create your CGI directory
This is where all script you wish to execute will go

sudo mkdir /var/www/cgi-bin

The next step tells Apache which directories it can execute in and how to handle a .py file when it sees one. With an editor open /etc/apache2/sites-available/default

sudo vi /etc/apache2/sites-available/default

Inside is a long list of configurations. We want to look at the block of text which should start around line 16. It looks like this:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin/">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

Change it to look like this:
(be sure to copy this carefully)

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py
    AddHandler default-handler .html .htm

Now save and exit.
Now it's time to make your homepage. Apache will automatically display /var/www /index.html, so we will use this file to make our home page.
Open the file with:

sudo vi /var/www/index.html

Delete everything inside.
Replace it with:

First Name:

Last Name:

Now that we have that, we need to make a script to parse it input and return a response.
Go to your cgi-bin directory and with an editor create

cd /var/www/cgi-bin
sudo vi

Here is the code we will be using for some sample tasks:


import cgi
import cgitb

Retrieve form fields

form = cgi.FieldStorage() # Get POST data
fname = form.getfirst("fname") # Pull fname field data
lname = form.getfirst("lname") # Pull lname field data
secret = form.getfirst("secretsquirrel") # Pull secretsquirrel field from POST data

Begin HTML generation

print "Content-Type: text/html; charset=UTF-8" # Print headers
print "" # Signal end of headers

HTML is pushed to the page with print

To save time you can use ''' to open and close a print

statement for block printing

print '''

print "First name:",fname
print "
print "Last name:",lname
print "
print "Whole name:",fname+" "+lname
print "
print "The secret was:",secret
print '''


Now allow the script to be executable:

sudo chmod +x

Restart apache2 so that config file changes take effect:
(This does not need to be done when changing site content, only apache2 config files)

sudo /etc/init.d/apache2 restart

answer Jul 29, 2013 by Manish Kumar SIngh
+1 vote

In order for you app to run as cgi, you would have to call the webapp2 run() function. Otherwise, it is going to implement wsgi interfaces.

Have a look at:

As well as:

answer Jul 28, 2013 by Luv Kumar
Similar Questions
+1 vote

I have some legacy binary code. Is there a way to run it on a current 64bit ubuntu system?

~$ file y
y: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.0.0, not stripped
~$ ./y
-bash: ./y: No such file or directory
0 votes

Pleas point me to a good source how to use that (with ubuntu and Tomcat 7)?

The internet is full of selfmade /etc/init.d/ scripts, but mostly it isn't used it as a real service (jsvc). Keyword: bin/**
Even the books "Apache Tomcat 7" and "Tomcat 7 Essentials" are not talking about it.

On the docs I've found this []:

tar xvfz commons-daemon-native.tar.gz
cd commons-daemon-1.0.x-native-src/unix
cp jsvc ../..
cd ../..

But what about:
* set env. variables (maybe:, /etc/profiles?)
* adjust the heap size
* logging to the right directory (like: /var/log/tomcat/...)
* specifying the User: "tomcat"

0 votes

I have a Ubuntu server running NGINX that logs data for me. I want to write a python script that reads my customized logs and after a little rearrangement save the new data into my DB (postgresql).

The process should run about every 5 minutes and i'm expecting large chunks of data on several 5 minute windows..

My plan for achieving this is to install python on the server, write a script and add it to cron.

My question is what the simplest way to do this? should I use any python frameworks?
For my python app I'm using Django, but on this server I just need to read a file, do some manipulation and save to DB.

+1 vote

I have a perl script I want to create desktop shortcut. While I click on this shortcut icon it will run my perl script and keep the terminal open.

I have tried the command

ln -s ~/Desktop/abc

while I click on this abc Icon from my desktop. It run and same time the terminal exit. Could you please help here?

My requirement is:

I want to create desktop shortcut for particular perl program. While I click on this icon from desktop, it should run and keep the terminal open.