[Tutor] NEWBIE is it possible to use HTML and Python together?

Kirby Urner urnerk@qwest.net
Fri, 22 Mar 2002 09:22:34 -0800


At 07:40 AM 3/22/2002 -0500, you wrote:
>I there any way to set the python up to run on a client side.
>I don think it's planned to run on the web, it's just his own
>little game. is there away to use something like py2exe to
>solve this?
>
>Thank you for your time in helping me solve this.
>Robert

Is there a web server in this picture at all -- even just
a local one on the same machine as the client?  It's
perfectly possible for a standalone machine not permanently
connected to the internet with static, public IP to run a
web server "facing inward" (i.e. to be a "host" and to
"run a web server" are not the same thing).

Perhaps your friend is using HTML to sort of mock up an
interface, i.e. is using the browser as a front end for
an app?  This is trendy, as various flavors of XML for
GUI-design are popping up and programmers will be taking
this approach quite a bit.

However, without a server in the picture, web pages need
something running client side to be dynamic.  Either that,
or use Python to take the place of the missing server,
i.e. you could write a tiny server in Python and point
the browser to it.  This is easier than it sounds.
However, you should make sure this code never handles
HTTP requests originating outside your box.

For example, put some html file (say thefile.html) in
your Python root directory and enter the following two
lines of code in IDLE.  Then point your browser to
http://127.0.0.1/thefile.html and it should come up in
your browser:

 >>> import SocketServer.CGIHTTPServer
 >>> SocketServer.ThreadingTCPServer(('127.0.0.1',80),\
       CGIHTTPServer.CGIHTTPRequestHandler).serve_forever()

You may need to kill your Python process from outside
when you're done with this demo.

A good resource here is 'The Python 2.1 Bible' Chapter 15,
from where I copied the above.  From here, you can go on
to write CGI handlers that handle requests.

Kirby