![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
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 |
|
|
#1 |
|
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() 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()
Good luck! Last edited by pmasiar; January 6th, 2007 at 12:55 AM.. |
|
|
|
|
|
#2 |
|
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 |
|
|
|
|
|
#3 | |
|
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:
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 |
|
|
|
|
|
|
#4 |
|
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...
|
|
|
|
|
|
#5 |
|
5 Cups of Ubuntu
![]() |
Re: Hello Web - simplest web app in python
|
|
|
|
|
|
#6 |
|
Quad Shot of Ubuntu
![]() 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 |
|
|
|
|
|
#7 | |
|
Gee! These Aren't Roasted!
![]() Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
|
Re: Hello Web - simplest web app in python
Quote:
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...
|
|
|
|
|
|
|
#8 |
|
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
|
|
|
|
|
|
#9 | |
|
Gee! These Aren't Roasted!
![]() Join Date: Oct 2006
Beans: 152
Ubuntu 6.10 Edgy
|
Re: Hello Web - simplest web app in python
Quote:
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...
|
|
|
|
|
|
|
#10 |
|
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
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
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|