python web programming for PHP programmers

N

Nikola Skoric

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

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?
 
D

Diez B. Roggisch

Nikola said:
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

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?

Try googling mod_wsgi. While "real" python web-programming is usually
done a bit different (django, turbogears, pylons and lots of others I
forgot), I think it comes close to what a PHP-programmer would expect
how a webapp begins.

It boils down to a python-file saying


def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

return [output]


Well, I did the googling - here you have a starter:


http://code.google.com/p/modwsgi/

See the configuration-section.

HTH,

Diez
 
A

ajaksu

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
 
D

D'Arcy J.M. Cain

a general python tutorial, I just need a tutorial on how to make a
hello world server side script with python. Any suggestions?

#! /usr/bin/env python

import sys, re

colour = re.sub('=', '=#', ''.join(sys.argv[1:]))

print """Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>

<HEAD>
<TITLE>Hello World</TITLE>
<LINK REV="MADE" HREF="mailto:[email protected]">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
CHARSET=utf-8"> </HEAD>

<BODY %s>
<H1>Hello World</H1>""" % colour

print """<P>
Hello World
</BODY></HTML>"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top