Porting 2.x to 3.3: BaseHTTPServer

Discussion in 'Python' started by Chris Angelico, Apr 21, 2013.

  1. 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
    Chris Angelico, Apr 21, 2013
    #1
    1. Advertising

  2. Chris Angelico

    Roy Smith Guest

    In article <>,
    Chris Angelico <> wrote:

    > 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?


    For most purposes, I would suggest one of the third-party web
    frameworks. For simple things, I'm partial to Tornado, but it's not the
    only choice. The advantage of these frameworks is they give you a lot
    of boilerplate code that handles all the low-level protocol gunk and
    lets you concentrate on writing your application logic.

    My gut feeling is that nobody should ever be using BaseHTTPServer for
    anything other than as a learning exercise (or as a base on which to
    build other frameworks). It's just too low level. I haven't used the
    3.x http.server, but http.server looks like much of the same.
    Roy Smith, Apr 21, 2013
    #2
    1. Advertising

  3. On Mon, Apr 22, 2013 at 12:01 AM, Roy Smith <> wrote:
    > In article <>,
    > Chris Angelico <> wrote:
    >
    >> 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?

    >
    > For most purposes, I would suggest one of the third-party web
    > frameworks. For simple things, I'm partial to Tornado, but it's not the
    > only choice. The advantage of these frameworks is they give you a lot
    > of boilerplate code that handles all the low-level protocol gunk and
    > lets you concentrate on writing your application logic.
    >
    > My gut feeling is that nobody should ever be using BaseHTTPServer for
    > anything other than as a learning exercise (or as a base on which to
    > build other frameworks). It's just too low level. I haven't used the
    > 3.x http.server, but http.server looks like much of the same.


    Have a look at the code in question:

    https://github.com/Rosuav/Yosemite/blob/master/Yosemite.py#L81

    It's REALLY simple. I don't need any sort of framework; it's basically
    just using a web browser as its UI, to save on writing a client. So
    I'm looking for the simplest possible option; I don't need security or
    anything (this is designed for a trusted LAN), nor scaleability (we're
    talking queries per hour, not per second).

    I'm actually looking at cutting it back even further. There are
    os.system() calls that I'm thinking should possibly become popen(),
    and maybe copy a binary into /tmp and giving a full path to it, as
    sometimes this is used on a low-end system and needs to perform
    actions with low latency.

    ChrisA
    Chris Angelico, Apr 21, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Molly
    Replies:
    0
    Views:
    500
    Molly
    Aug 25, 2003
  2. Joshua W. Biagio
    Replies:
    0
    Views:
    582
    Joshua W. Biagio
    Sep 30, 2003
  3. Yin
    Replies:
    1
    Views:
    391
    Eddie Corns
    Jan 26, 2004
  4. David
    Replies:
    1
    Views:
    856
    Christos TZOTZIOY Georgiou
    Jul 28, 2004
  5. Serhiy Storchaka

    Re: Porting 2.x to 3.3: BaseHTTPServer

    Serhiy Storchaka, Apr 21, 2013, in forum: Python
    Replies:
    0
    Views:
    94
    Serhiy Storchaka
    Apr 21, 2013
Loading...

Share This Page