Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old January 5th, 2007   #1
pmasiar
Day Old Decaf
 
Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
Hello Web - 2 simple web apps in python (3 lines and 35 lines)

Someone complained that Python web app tutorials are harder than PHP. So I created these two simple python apps to help you started.

I assume you have cgi-enabled directory.

UPDATE 1: Find your python first. In terminal, type 'which python'.

Then change first line of following scripts according where your python is: /usr/bin/python or /usr/local/bin/python or whatever you have.
(end update1)


Simplest one (3 lines): place it in CGI-enabled directory, make executable by web user (or anybody, or all):

Code:
#!/usr/bin/python
import cgi
cgi.test()
It will print your CGI environment variables and you can be sure all is OK. If not, error is in your web server settings. Also, make sure that apache can read/execute this script: ie. make ir readable/executable by all.

More complicated (35 lines including 13 lines of HTML template), but still simple:

Code:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
import os

##---CONSTANTS -------
BGCOLOR = '#f0f0f0'

##---TEMPLATES ----------------------------------------------------
HTML = """content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>%(TITLE)s</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body bgcolor="%(COLOR)s"><h1>%(TITLE)s</h1>
    <h2>Hello %(NAME)s</h2>
    <hr /><h2> Who are you?</h2>
    <form action="%(SCRIPT_NAME)s" method="POST" enctype="multipart/form-data">
        <input name="person_name" type="text" size="40"><br />
        <input name="submit" type="submit" value="Try">
    </form>
</body></html>"""

def main():
    FORM = cgi.FieldStorage()
    person = FORM.getfirst('person_name')
    if not person:
        person = '(Nobody yet)'
    next_script = os.environ['SCRIPT_NAME'] # name of this script: it submits to self

    print HTML % dict(TITLE='Hello Web!', NAME=person, 
        SCRIPT_NAME=next_script, COLOR=BGCOLOR)

# idiomatic start of the main() program
if __name__ == '__main__': main()
It will ask for name, and after hitting submit, prints it in the page and asks for more. If you have this running, you are on the way.

Good luck!

Last edited by pmasiar; January 6th, 2007 at 12:55 AM..
pmasiar is offline   Reply With Quote
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:13 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry