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

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
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
Old January 5th, 2007   #2
gummibaerchen
Gee! These Aren't Roasted!
 
Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Ohh, noone should use CGI in the way you offered.
Maybe a quick start, but not a good one.

That's as lame as PHP + HTML.

A Python Web-Application should be written after WSGI norms.

http://en.wikipedia.org/wiki/WSGI

Then their may be and interface for CGI if nothing better, as mod_python for example, is available.

Ok, WSGI looks harder than PHP + HTML, but in the end it's much better than pure CGI programming.

But considering, that you just want to be simple, you're damn right
It's simple, but not nice.

I think you can do a lot better, but maybe for newcomers it's nice, as they want a quick result. But I wouldn't recommend this way.
__________________
Edgy Eft Packages: Glade-3 3.1.4, Geany 0.10, Banshee 0.11.3, Inkscape 0.45, [] Geom - interactive geometry []
Any questions? Send me a PM, as I don't subscribe to every thread.
Mark Shuttleworth...

Last edited by gummibaerchen; January 5th, 2007 at 09:38 PM.. Reason: typo
gummibaerchen is offline   Reply With Quote
Old January 5th, 2007   #3
pmasiar
Day Old Decaf
 
Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Quote:
Originally Posted by gummibaerchen View Post
Ohh, noone should use CGI in the way you offered.
...
But considering, that you just want to be simple, you're damn right
It's simple, but not nice.

I think you can do a lot better, but maybe for newcomers it's nice, as they want a quick result. But I wouldn't recommend this way.
Agree with you 100%. We can do better - after learning basics.

My first goal was to test CGI is configuration, 3 line script is good enough, and if it does NOT work, your WSGI/mod_python goodness will not work either, it just will be LESS obvious what is wrong.

I am in process with one python beginner to sorting out why 1st example fails (she is on shared hosting) - some misconfigured CGI.

Second example was to show interactiive component without confusing beginner with templating system.

My 3rd example was supposed to use simples templating (htmltmpl) with the same template and multi-line string in 2nd example but I run out of time
pmasiar is offline   Reply With Quote
Old January 7th, 2007   #4
gummibaerchen
Gee! These Aren't Roasted!
 
Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Yeah, you're totally right.

Maybe you should extend your 2 Examples with an interactive one, that may interested more people.

For example http://example.com/cgi-bin/sqrt.py/81
And the output is

Code:
<h1>The Squareroot of 81 is 9</h1>
__________________
Edgy Eft Packages: Glade-3 3.1.4, Geany 0.10, Banshee 0.11.3, Inkscape 0.45, [] Geom - interactive geometry []
Any questions? Send me a PM, as I don't subscribe to every thread.
Mark Shuttleworth...
gummibaerchen is offline   Reply With Quote
Old January 7th, 2007   #5
lazka
5 Cups of Ubuntu
 
Join Date: Aug 2006
Location: Austria
Beans: 44
Send a message via ICQ to lazka
Re: Hello Web - simplest web app in python

http://webpython.codepoint.net/mod_p...er_hello_world
lazka is offline   Reply With Quote
Old January 7th, 2007   #6
maddog39
Quad Shot of Ubuntu
 
maddog39's Avatar
 
Join Date: Mar 2006
Location: Pennsylvania, US
Beans: 452
Ubuntu 9.10 Karmic Koala
Re: Hello Web - simplest web app in python

I finally got these two examples working, and here they are.

http://www.dx-h.com/cgi-bin/cgitest.py - Example 1
http://www.dx-h.com/cgi-bin/cgitest2.py - Example 2
__________________
Ubuntu User #: 0x2695 | Banshee 1.0 Pidgin Plugin

Lenovo IdeaPad S10, 1.6GHz Intel Atom, Ubuntu 9.10 Netbook Remix
maddog39 is offline   Reply With Quote
Old January 8th, 2007   #7
gummibaerchen
Gee! These Aren't Roasted!
 
Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Quote:
Originally Posted by maddog39 View Post
I finally got these two examples working, and here they are.

http://www.dx-h.com/cgi-bin/cgitest.py - Example 1
http://www.dx-h.com/cgi-bin/cgitest2.py - Example 2
Example 1 isn't working for me

Would you pls also show the source? That may be interesting
__________________
Edgy Eft Packages: Glade-3 3.1.4, Geany 0.10, Banshee 0.11.3, Inkscape 0.45, [] Geom - interactive geometry []
Any questions? Send me a PM, as I don't subscribe to every thread.
Mark Shuttleworth...
gummibaerchen is offline   Reply With Quote
Old January 8th, 2007   #8
pmasiar
Day Old Decaf
 
Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Quote:
Originally Posted by gummibaerchen View Post
Example 1 isn't working for me

Would you pls also show the source? That may be interesting
Both examples work for me as supposed. Good job! Source code is in #1 - parent posting.
pmasiar is offline   Reply With Quote
Old January 8th, 2007   #9
gummibaerchen
Gee! These Aren't Roasted!
 
Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Quote:
Originally Posted by pmasiar View Post
Both examples work for me as supposed. Good job! Source code is in #1 - parent posting.
http://www.dx-h.com/cgi-bin/cgitest.py - Example 1

That one gives me a long list of errors
__________________
Edgy Eft Packages: Glade-3 3.1.4, Geany 0.10, Banshee 0.11.3, Inkscape 0.45, [] Geom - interactive geometry []
Any questions? Send me a PM, as I don't subscribe to every thread.
Mark Shuttleworth...
gummibaerchen is offline   Reply With Quote
Old February 21st, 2007   #10
pmasiar
Day Old Decaf
 
Join Date: Jun 2006
Location: CT, USA
Beans: 5,268
Ubuntu 6.10 Edgy
Re: Hello Web - simplest web app in python

Quote:
Originally Posted by gummibaerchen View Post
Example 1....gives me a long list of errors
They are not errors - cgi.test() is supposed to print all environment variables, and it does exactly that. How hard can be to understand program 3 lines long with just one library call? Sometimes even that is not easy
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 08:53 AM.


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