[Techtalk] How to write web proxy in Python?

Mary mary-linuxchix at puzzling.org
Fri Jan 10 09:16:18 EST 2003


Hi Amanda,

LinuxChix has a programming list, so I've Cc-ed them. If people reply to
the lists, please reply to the programming list (archives are open, so
you can read responses at
http://mailman.linuxchix.org/pipermail/programming/ if you aren't
subscribed).

Programmers: refer to
http://mailman.linuxchix.org/pipermail/techtalk/2003-January/017251.html
for Amanda's original post.

On Thu, Jan 09, 2003, Amanda Babcock wrote:
> I have heard that the fastest language to write a webserver in is
> Python.  I figure the same would be true for a proxy.  I have Python
> on my box, but I don't know the language, and I've never done socket
> programming in any language.  I don't require the proxy to accept
> multiple clients - pages will display in one page of the browser,
> though it does have frames (does that make it look like multiple
> clients?).

I would suggest using something higher level than sockets if you
possibly can, unless the assignment requires sockets. Python has a
socket module which is easier to use than the C one - it is slightly
higher level and handles some of the errors itself, so I'd recommend it
over C, since you've never programmed sockets before.

If the assignment doesn't require sockets, Python has an httplib module
that might be appropriate, which will open connections for you without
you needing to open and use sockets yourself.

You might even be able to use the urllib module instead, which will
fetch a url for you with a single line of code.

To make the proxy, I would suggest you look at Python's SimpleHTTPServer
module, or maybe BaseHTTPServer and override some methods, so instead of
reading a page off disk, it requests the page from the network and does
your replacement (Python's string module will handle the replacement).

For a beginning Python programmer, I'd suggest working through the
tutorial at http://www.python.org/doc/current/tut/tut.html

All modules I've mentioned are documented at
http://www.python.org/doc/current/modindex.html

-Mary



More information about the Techtalk mailing list