[Edu-sig] Writing a web server

Brent Burley and Pat Erb erburley@ix.netcom.com
Sun, 27 May 2001 10:24:30 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C0E697.3715CE40
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

One of the things that is _really_ easy to do in Python is writing a web
server.  In fact, the standard distribution comes with a fully functioning
server as an example.  Find your lib directory and run SimpleHTTPServer.py
directly and then open http://localhost:8000/ in your web browser.  This web
server provides a simple server-side file browser that can navigate
directories and serve up html, graphics, and text files.  This should work
on most computers, including Windows, and you don't even need an Internet
connection.  Try it!

I've attached an even simpler web server (16 lines of code) that is the web
equivalent of "hello world".  As simple as it is, it's complete enough that
it shows everything you need to build a dynamic website.  Just send
"text/html" instead of "text/plain" and generate an html file dynamically in
the do_GET method.  The "self.path" variable contains the requested URL and
can encode any information you want to pass between requests.

How's this for a possible 9th/10th grade course:
* Web Programming with Python - Students will program a fully-functioning
website from scratch.  Students will choose from building an interactive
storybook where the reader influences the outcome of the story, or a
web-community site with a message board and voting booth.  No prior
programming experience necessary.

I think there are many advantages to this approach.  For one thing, students
can get immediate feedback through the web browser and can easily include
graphics, sound, and simple user-interfaces via html forms.  If the school
allows the projects to be posted to a public server, then the students' work
could be shown off to friends, relatives, and the world at large.  This
teaches directly marketable, real world skills and teaches programming as a
means to an end.  I also have an inkling (based on my niece and her friends)
that this would appeal strongly to girls.

Thoughts?

    Brent Burley

------=_NextPart_000_0008_01C0E697.3715CE40
Content-Type: text/plain;
	name="webserve.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="webserve.py"

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyServerHandler(BaseHTTPRequestHandler):
    def do_HEAD(self):
        self.send_head()
    def do_GET(self):
        self.send_head()
        self.wfile.write("Hello, your request was: " + self.path)
    def send_head(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()

server =3D HTTPServer(('', 8000), MyServerHandler)
print "Web server running... direct your web browser to =
http://localhost:8000/hello"
server.serve_forever()

------=_NextPart_000_0008_01C0E697.3715CE40--