python web programming for PHP programmers

ajaksu ajaksu at gmail.com
Wed Dec 24 16:49:53 CET 2008


On Dec 24, 12:40 pm, Nikola Skoric <nick-n... at net4u.hr> wrote:
> I0m a python newbie with PHP background. I've tried to make a web app
> from one of my python scripts (which I haven't done before) and I
> ended up with:
>
> <?php
> echo shell_exec("python foobar.py");
> ?>
> which works really nice :-D

Clever :)

Python can work in a similar way to PHP if your server supports it,
but Python also allows you to create the server itself.


> For some reason I can't find no "quick and dirty python web
> programming tutorial for PHP programmers" on google. :-D I don't need
> a general python tutorial, I just need a tutorial on how to make a
> hello world server side script with python. Any suggestions?

See http://webpython.codepoint.net/cgi_hello_world for a very
primitive way (and docs here: http://docs.python.org/dev/library/cgi.html
).

A better way, that uses a trivial Python-based server:

hello.py
---
from wsgiref.simple_server import make_server
def hello_app(environ, start_response):
    start_response("200 OK", [('Content-Type','text/plain')])
    return "Hello world!"
httpd = make_server('', 8000, hello_app)
print "Serving HTTP on port 8000..."
httpd.serve_forever()
---

Then:
$ python hello.py
Serving HTTP on port 8000...
localhost - - [24/Dec/2008 13:11:32] "GET / HTTP/1.1" 200 12
(see http://docs.python.org/dev/library/wsgiref.html#module-wsgiref.simple_server
)

You can use better Python-based servers with handy features for
testing:
http://projects.wagner-flo.net/wsgi-serve/
http://pypi.python.org/pypi/James/0.7.1


Now, to ease things, you can have Python working more like PHP.

First, server support. What HTTP server are you using? For starting
up, mod_python (http://www.modpython.org/) isn't that bad, but there
are better alternatives: http://code.google.com/p/modwsgi/ ,
http://pypi.python.org/pypi/python-fastcgi/1.1 and http://python.ca/scgi/

Then, there are many ways of getting the PHP feel (embedding in pages,
etc.):
http://www.modpython.org/live/current/doc-html/pyapi-psp.html#pyapi-psp
http://nick.borko.org/pse/examples/php_example.html
http://snakelets.sourceforge.net/ (development stopped)
http://www.webwareforpython.org/PSP/Docs/index.html

HTH,
Daniel



More information about the Python-list mailing list