[Tutor] Python for Web Applications - Off Topic

Pedro Diaz Jimenez pdiaz88@terra.es
Wed, 13 Jun 2001 21:42:50 +0000


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Funny he/she didn't Zope, right?

Cheers
Pedro

On Wednesday 13 June 2001 17:01, DavidCraig@PIA.CA.GOV wrote:
> This came out this morning from Open Source  - Interesting
>
> ONE SIZE FITS MOST
>
> Posted at June 8, 2001 01:01 PM PST Pacific
>
>
> THIS WEEK I finally make good on my promise to take a
> closer look at Python for building Web applications.
>
> Python is a Javalike language in some of the most
> important respects. It is byte-compiled and therefore
> platform-neutral. It sports automatic garbage
> collection, which means you won't have to worry about
> cleaning up after Python objects when you're done with
> them. And it handles exceptions well, enabling you to
> make your programs more robust.
>
> Unlike Java, Python does not need type declarations,
> which means a variable becomes a string type simply by
> setting it to a string value. That is one reason it
> seems to take a lot less work to crank out a quick and
> dirty application in Python than it does using Java.
>
> Python has a long list of available expansion modules
> to give your Python Web application access to LDAP,
> IMAP4 (Internet Messaging Access Protocol 4), POP
> (Post Office Protocol), SMTP, NNTP (Network News
> Transfer Protocol), among other Internet resources.
> There is also a number of generic database interfaces
> to popular SQL servers. If Web applications aren't
> your thing, you can still use Python to create
> anything from simple command-line scripts to
> full-blown graphical applications using the Troll Tech
> Qt GUI toolkit.
>
> You can use several methods to add Python capability to
> your Web server, and they are not necessarily mutually
> exclusive. I have set up my servers to run Python in a
> number of different ways, according to need.
>
> The simplest and most efficient way to add Python to
> Apache is to use the mod_python interface, available
> at www.modpython.org. When you use this module, you
> generally publish information to the browser by using
> the "req.write()" command.
>
> This Apache module isn't terribly useful by itself, but
> it comes with a mod_python publisher module that makes
> it much easier to use Python to write Web
> applications. When you configure Apache to use the
> mod_python publisher, you can execute Python functions
> by pointing to the Python application, followed by the
> function you want to run. For example, if you want to
> run the "say_hello" function in the "welcome.py"
> program, point your browser to
> "http://somesite.domain/welcome/say_hello."
>
> The only drawback to the publisher module is that it
> writes information to the browser based only on the
> information a function returns. This means that you
> have to create a Web page by assembling a long string
> or an array and then passing it to the browser all at
> once at the end of the function.
>
> This approach can work to your advantage, depending on
> the type of application you are building. If you must
> assemble each page by combining parts into a whole,
> you will probably spend more time thinking about which
> parts to create. You are likely to think through how
> you should break up your Web site content so that you
> end up with a site that doesn't break the first time
> you need to add a module or change the appearance of a
> page.
>
> Of course, that doesn't mean you can't be sloppy if you
> want to be. If you're spoiled on the freedom afforded
> by a Web language such as PHP (Hypertext PreProcessor,
> www.php4.org), you can always use PSP. I used PSP via
> a toolkit known as Webware for Python
> (webware.sourceforge.net). PSP uses almost the same
> syntax as Java Server Pages (JSP), so it's easy to
> bounce between the two if you're used to one or the other.
>
> At the other extreme, the WebKit portion of Webware for
> Python really carries Python into the big leagues for
> Web application development. WebKit includes a
> multithreaded application server that runs alongside
> Apache. The WebKit server serves up the Python
> applications more intelligently than mod_python alone
> or the mod_python publisher. In fact, although WebKit
> can use mod_python as its default Python extension to
> Apache, WebKit comes with its own Apache module in
> case you don't want or need mod_python.
>
> If you are serious about using Python to develop for
> the Web, I strongly recommend that you look at WebKit
> and the sample applications. The samples aren't
> pretty, but they demonstrate the power of Python's
> object-oriented nature when it comes to building the
> framework for a Web application and then extending it
> as needed.
>
> Be sure to visit the online version of this column for
> examples of Python Server Pages, mod_python, and the
> mod_python publisher if you want a closer look. I also
> will be publishing detailed examples on VarLinux.org
> (www.varlinux.org).
>
> Finally, some readers have pointed me to Ruby, a
> Pythonlike language which is yet another alternative.
> I haven't yet had the time to check it out, but when I
> do I'll let you know what I find.
>
> PSP example
>
> Here is an example of how to Python within a server
> page, my least favorite way to use Python, I might
> add. This file could be called "welcome.psp," which
> has the .psp extension to alert Apache that this is a
> Python Server Page.  For a sample Apache configuration
> to run PSP, see the last example at the bottom.
>
> <html> <head> <title>Welcome</title> </head> <body> <%
> title = "<H1>Welcome to the world of Python</H1>"
> greeting = "If you lived here, you'd be home by now"
> res.write(title) res.write("<P>")res.write(greeting)
> %></body></html>
>
>
>
> mod_python example
>
> Here is an example of using Python with the simplest
> mod_python approach.  You would create a file called
> "welcome.py" with the following content.
>
> from mod_python import apache
>
> def handler(req): title = "<H1>Welcome to the world of
> mod_python</H1><P>"greeting = "If you lived here, etc."
>
>
>
> req.content_type = "text/html"req.send_http_header()
> req.write(title) req.write(greeting)
>
>
>
> return apache.OK
>
> Then you must create an entry for this file in your
> Apache configuration file "httpd.conf".  That entry
> might look something like the following, assuming you
> have named the above file "welcome.py" and put it in a
> directory called "/python." Note that it is the
> "PythonHandler" statement that points to the
> welcome.py file (the ".py" extension is not needed
> here since it is assumed).
>
> <Directory /python> AddHandler python-program .py
> PythonHandler welcome </Directory>
>
>
>
> The browser accesses this Python file with a URL such
> as "http://somesite.domain/welcome." The mod_python
> module automatically executes the handler function for
> the file "welcome.py."
>
> In most cases, this isn't a terribly useful means of
> using Python for Web applications because there can be
> only one Python handler file for each directory.  A
> reasonable application would span many directories,
> each with its own handler.
>
> mod_python example using Publisher Handler
>
> The mod_python module comes with a Publisher Handler
> that allows you to create Web applications using fewer
> Python files and directories.  You can create many
> functions for your Python programs, and you can
> reference these functions directly through URLs. For
> example, if you want to execute the "say_hello"
> function in the "welcome.py" program, you would use
> the URL "http://somesite.domain/welcome/say_hello."
> Use the URL
> "http://somesite.domain/welcome/say_goodbye" to access
> the "say_goodbye" function.
>
> For the simplest configuration, just add lines such as
> these to your Apache configuration file to activate
> the publisher.
>
> AddHandler python-program .py PythonHandler
> mod_python.publisher
>
>
>
> Then you can create "welcome.py" to include the
> following.
>
>
>
> def say_goodbye():s = "<H1>Thanks for visiting!</H1>"
> return s
>
>
>
> def say_hello(): h1open = "<H1>" h1close = "</H1>" s =
> h1open + "Welcome to mod_python publisher." + h1close
> return s
>
>
>
> The mod_python publisher sends text, HTML, or whatever
> you choose to a browser depending on what a function
> returns. In the example above, each function returns
> string "s," which contains different text depending on
> the function.
>
> Combinations
>
> Here is a sample way to configure Apache if you want to
> use Python according to your varying needs. This
> configuration allows you to use mod_python to run
> either mod_python publisher-based applications, Python
> Server Pages, or the more sophisticated Webware
> applications via WebKit. You should adjust the
> configuration to work with the way you have installed
> software on your particular server. My test system is
> Debian-based, which is why the Web data is placed
> under the "/var/www" directory.
>
> <IfModule mod_python.c> AddHandler python-program .psp
> PythonPath "sys.path+['/var/www/Webware/WebKit']"
> PythonHandler ModPythonAdapter::pspHandler PythonDebug
> On
>
>
>
> Alias /python/ /var/www/python/ <Location
> /python>AddHandler python-program .py SetHandler
> python-program PythonPath
> "sys.path+['/var/www/python', '/python']"
> PythonHandler mod_python.publisher Options +ExecCGI
> PythonDebug On </Location>
>
>
>
> Alias /webware/ /var/www/Webware/WebKit/ <Location
> /webware> AddHandler python-program .py SetHandler
> python-program # add the directory that contains
> ModPythonAdapter.py PythonPath
> "sys.path+['/var/www/Webware/WebKit']" PythonHandler
> ModPythonAdapter Options +ExecCGI PythonDebug On
> </Location> </IfModule>
>
> Nick Petreley is the founding editor of VarLinux.org
> (www.varlinux.org) and LinuxWorld. Reach him at
> nicholas@petreley.com.
> D. H. Craig, CSM
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

- -- 

/*
 * Pedro Diaz Jimenez
 * pdiaz88@terra.es 
 * pdiaz@acm.asoc.fi.upm.es
 *
 * Wanna see how 100000! looks like?:
 * http://acm.asoc.fi.upm.es/~pdiaz/fact_100.000
 * 
 * La sabiduria me persigue, pero yo soy mas rapido
 * 
 * "Las artes marciales son parte de una filosofía,
 *  no deben ser consideradas un arma. Y por eso,
 *  recuerda: No hay nada como un buen revolver"
 *    Les Luthiers, Iniciacion a las Artes Marciales
 *
 */

Random quote:
- -------------

ROMEO:		Courage, man; the hurt cannot be much.
MERCUTIO:	No, 'tis not so deep as a well, nor so wide
			as a church-door; but 'tis enough, 'twill serve.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7J95gnu53feEYxlERAo3EAJ9GFAhrJ79LEN+G8bEBWi8Xd42VCQCdFWk8
kGwLSWa+83hLNo1TPO/brTk=
=iiGY
-----END PGP SIGNATURE-----