[Tutor] Python Scripting

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Jan 23 02:24:50 CET 2005



On Sat, 22 Jan 2005, Ali Polatel wrote:


>    I want to ask you something that I am really curious about.
>    Can I design web-pages with python or use py files to write html?

Hi Ali,

Almost every programming language allows us to write strings into files,
so from an academic standpoint, 'yes'.  *grin*

>From a practical one, it also looks like 'yes', because there's quite a
few good libraries to do this kind of HTML writing.  Python.org has a
catalog of these "HTML Preprocessors" here:

    http://www.python.org/moin/WebProgramming



>    if the answer is yes and if I upload some scripts to the web-site with* .py
>    does someone have to ahve python interepreter in his computer to be
>    able to view those pages?

Since the WWW provides communication between clients and servers,
programmers have developed two main strategies for playing with the WWW.


    o.  Client-side programming: this means that the server will send over
Python programs; clients then are responsible for running those programs.
This means that the clients have to have Python installed.  This also
means that the server doesn't necessarily have to have Python installed.
Less work on the server, and more work on the clients.

    o.  Server-side programming: this means that the server will run
Python programs on the server side, and send the program's printed output
to the client.  This means that the clients don't need to have Python
installed.  More work on the server, and less work on the clients.


A lot of systems these days are opting toward server-side programming
because it's more transparently easy for clients to use.  CGI programming
is server-side.  In fact, as long as the server can run Python, the
clients don't even have to know that it's Python that's generating a web
page.  As a concrete example: people who visit a url like:

    http://maps.yahoo.com/

may not even realize that (at least from April 2001) the maps.yahoo.com
web site was being run on Python programs.
(http://mail.python.org/pipermail/tutor/2001-April/004761.html)



> And is it possible to write a programme with python which will update a
> webpage by the commands it receives from a server ( I mean I am writing
> a bot for a chess server and I want it to be able to update a web-page
> about the last results of games etc.)

Yes.  In fact, you probably don't need to worry about CGI at all for your
particular project.  You can just write a program that writes out a static
HTML page.  If you run that program every so often, the generated page
will appear to be updating periodically.

For example, something like:

###
import datetime
f = open("last_time.html", "w")
f.write("""<html><body>
<p>The last time the program was called was at %s
</body></html>""" % datetime.datetime.today())
f.close()
###

will write out a web page 'last_time.html' whose content should change
every time we run the program.  So what you know already should be enough
to write a program to track server statistics: your chess bot can
periodically write updates as an HTML file.



If you want a program to run every time a person visits a web page --- to
generate a page on demand --- then that dynamic approach requires a little
more work, and that's where web programming systems like CGI comes into
play. The topic guide here:

    http://www.python.org/topics/web/

should have some tutorials that you can look at.


If you have more questions, please feel free to ask!



More information about the Tutor mailing list