Porting 2.x to 3.3: BaseHTTPServer

Chris Angelico rosuav at gmail.com
Sun Apr 21 09:46:27 EDT 2013


I'm porting an old project to Python 3, with the intention of making
one codebase that will still run on 2.6/2.7 as well as 3.2+ (or 3.3+,
if 3.2 is in any way annoying). My first step was to run the code
through 2to3, and the basics are already sorted out by that. Got one
question though, and it's more of an advice one.

In the current version of the code, I use BaseHTTPServer as the main
structure of the request handler. 2to3 translated this into
http.server, which seems to be the nearest direct translation. But is
that the best way to go about making a simple HTTP server?

Also, it's expecting bytes everywhere, and I can't find a simple way
to declare an encoding and let self.wfile.write() accept str. Do I
have to explicitly encode everything that I write, or is there a
cleaner way? (I could always make my own helper function, but would
prefer something standard if there's a way.)

The current version of the code is at: https://github.com/Rosuav/Yosemite

It's ugly in quite a few places; when I wrote most of that, I was
fairly new to Python, so I made a lot of naughty mistakes (bare except
clauses all over the place, ugh!). Adding support for Python 3 seems
like a good excuse to clean all that up, too :)

ChrisA



More information about the Python-list mailing list