A Digital Age Deserves A Digital Leader

Python web programming

Python web programming

Postby gries818 » Thu Mar 26, 2009 8:19 pm

I've recently rediscovered Python and found out that it can be used as a web scripting language as well, but I've had little luck getting it to work well on my server.

I have mod_python installed on my Apache installation and can choose between regular python files and psp files (scripting python inside html... like PHP or classic ASP), but both reject most of the standard Python code I try to use. Even this little script (taken from the Python tutorial) that gives a few numbers in the Fibonacci sequence fails:

Code: Select all
a, b = 0, 1
while b < 10:
    print b
    a, b = b, a+b


In terms of web programming, PHP is my "native" language and I've grown rather used the extensive documentation on PHP. Every obscure function and use is documented and explained in detail; Python's web scripting documentation seems utterly deficient in comparison.

So far, I've gathered most people who develop web apps with Python use a "web framework". I've probably been out of it, since I don't actively work as a programmer, but I had never even heard of the term until two days ago, when I started researching Python. Since then, I've gathered there are web frameworks for PHP. Is it even possible to develop PHP quality web apps with Python without a framework?

It isn't a really big deal to me to develop in Python anyway... just thought it would be another cool thing to learn. Any help would be appreciated!
Image

Mac OS 10.6.7 - Personal
Ubuntu Server 11.04 - Server
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Re: Python web programming

Postby gries818 » Sat Mar 28, 2009 6:09 pm

Well I still don't understand everything I would need to know to actually develop an application, but I have made some progress since I posted this two days ago.

I've managed to setup systems on both Linux and Windows machines. Most Linux machines are bundled with an appropriate version of Python so that isn't a concern. My edition of Ubuntu server even came with mod_python included, I just had to enable it. Unfortunately, I can't be that much help to anyone looking follow me because I did it before I posted the original post and can't really remember exactly what I did to get it to work. Oh well; this SITE was helpful to me in getting mod_python enabled, but the instructions don't work exactly so be prepared to do a little tweaking.

Getting it setup on Windows was a bit more problematic. The newest, non 3.x, version of Python doesn't work with the newest version of mod_python. I had to downgrade to version 2.5.4. Add the code on the Windows version to httpd.conf:

Code: Select all
LoadModule python_module modules/mod_python.so


This code will need to be added to both the Windows and Linux versions in whatever the server uses as its main configuration file (switch out the location name for the desired folder - Linux version doesn't require quotes):

Code: Select all
<Directory "C:/xampp/htdocs/">
       AddHandler mod_python .py
       PythonHandler mod_python.publisher
       PythonDebug On
</Directory>


Restart Apache and .py file should work. For whatever reason though, there is a modified version you must use for web development... or at least I can't get it to work any other way. Code must start with a new function:

Code: Select all
def index(req):


And you can't use the print command/function you would use in normal scripts. Instead you must use req.write(). Here is an example of the equivalent Fibonacci script I showed above:

Code: Select all
def index(req):
    req.content_type = 'text/html'
    req.write("<html>\n\n")
    req.write("<head>\n")
    req.write("<title>Python Testing</title>\n")
    req.write("</head>\n\n")
    req.write("<body>\n\n")

    a, b = 0, 1
    while b < 1000000:
        req.write(str(b))
        req.write("<br />\n")
        a, b = b, a+b

    req.write("\n\n</body>\n")
    req.write("</html>")


I've had much less luck with the inline Python or PSP route. Basically the only thing I can seem to accomplish is this:

Code: Select all
<%= "Hello World!" %>


Which if I remember correctly, is essantially the same as ASP code. If you'd like to try your hand at PSP, use this code instead:

Code: Select all
<Directory "C:/xampp/htdocs/">
    AddHandler mod_python .psp
    PythonHandler mod_python.psp
    PythonDebug On
</Directory>


Hope this saves others some time ^*^ ... isn't PHP just easier :yesnod:
Image

Mac OS 10.6.7 - Personal
Ubuntu Server 11.04 - Server
Software Development
User avatar
Posts: 3991
Joined: Wed Jul 07, 2004 6:28 pm

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 1 guest