Newbie question about a web server

Discussion in 'Python' started by Frank Millman, Aug 20, 2007.

  1. Hi all

    I have just started to dabble in writing my own web server.

    I googled for 'python web server', and this is the first hit -

    http://fragments.turtlemeat.com/pythonwebserver.php

    It has the source code for a simple web server, based on HTTPServer
    and BaseHTTPRequestHandler.

    It demonstrates the concepts of
    - returning a static page
    - constructing and returning a dynamic page
    - sending a form with a POST method, and responding to the result

    I typed it in and ran it, and it 'just worked', with one exception.

    When responding to the initial request, it sends a 200 response,
    followed by a content-type header, followed by an html page. This
    works fine.

    When responding to the POST data received, it sends a 301 response, no
    headers, and then the html page.

    This works with a Firefox browser on Linux, but MSW IE6 displays 'the
    page cannot be displayed'.

    According to the notes, "You don't have to know much about the HTTP
    protocol at all. Except some basic that when the client request
    something it is a "GET", and when the client sends something it is in
    our case a POST. Some basic responce codes like 200 is OK for GET, and
    404 is file not found, 301 is OK for a Post."

    I googled for 'http response code 301', and found that it is actually
    a redirection code. It seems that the notes are misleading.

    So I guess my questions are -
    1. what is the correct response for a POST?
    I think the answer is 200.
    However, I assume the author is trying to demonstrate a
    particular technique.
    Can anyone explain what that may be.
    2. why does the above work with Firefox?

    TIA for any enlightenment.

    Frank Millman
    Frank Millman, Aug 20, 2007
    #1
    1. Advertising

  2. Frank Millman

    Chris Allen Guest

    On Aug 20, 12:02 am, Frank Millman <> wrote:
    > Hi all
    >
    > I have just started to dabble in writing my own web server.
    >
    > I googled for 'python web server', and this is the first hit -
    >
    > http://fragments.turtlemeat.com/pythonwebserver.php
    >
    > It has the source code for a simple web server, based on HTTPServer
    > and BaseHTTPRequestHandler.
    >
    > It demonstrates the concepts of
    > - returning a static page
    > - constructing and returning a dynamic page
    > - sending a form with a POST method, and responding to the result
    >
    > I typed it in and ran it, and it 'just worked', with one exception.
    >
    > When responding to the initial request, it sends a 200 response,
    > followed by a content-type header, followed by an html page. This
    > works fine.
    >
    > When responding to the POST data received, it sends a 301 response, no
    > headers, and then the html page.
    >
    > This works with a Firefox browser on Linux, but MSW IE6 displays 'the
    > page cannot be displayed'.
    >
    > According to the notes, "You don't have to know much about the HTTP
    > protocol at all. Except some basic that when the client request
    > something it is a "GET", and when the client sends something it is in
    > our case a POST. Some basic responce codes like 200 is OK for GET, and
    > 404 is file not found, 301 is OK for a Post."
    >
    > I googled for 'http response code 301', and found that it is actually
    > a redirection code. It seems that the notes are misleading.
    >
    > So I guess my questions are -
    > 1. what is the correct response for a POST?
    > I think the answer is 200.
    > However, I assume the author is trying to demonstrate a
    > particular technique.
    > Can anyone explain what that may be.
    > 2. why does the above work with Firefox?
    >
    > TIA for any enlightenment.
    >
    > Frank Millman



    Yes HTTP response code 200 does indicate a normal response. What you
    really need to look at to build an HTTP server is is the RFC for
    HTTP. It is HTTP 1.1 is RFC 2616 and you can view it here:
    http://www.faqs.org/rfcs/rfc2616.html

    That should get you on your way...
    Chris Allen, Aug 20, 2007
    #2
    1. Advertising

  3. Frank Millman

    I V Guest

    On Mon, 20 Aug 2007 00:02:47 -0700, Frank Millman wrote:
    > When responding to the POST data received, it sends a 301 response, no
    > headers, and then the html page.

    [...]
    > According to the notes, "You don't have to know much about the HTTP
    > protocol at all. Except some basic that when the client request
    > something it is a "GET", and when the client sends something it is in
    > our case a POST. Some basic responce codes like 200 is OK for GET, and
    > 404 is file not found, 301 is OK for a Post."

    [...]
    > I googled for 'http response code 301', and found that it is actually
    > a redirection code. It seems that the notes are misleading.

    [...]
    > So I guess my questions are -
    > 1. what is the correct response for a POST?
    > I think the answer is 200.
    > However, I assume the author is trying to demonstrate a
    > particular technique.
    > Can anyone explain what that may be.


    The code at that site looks wrong to me; I suspect the original author
    was confused about status codes. If you return a 301 redirect code, you
    should include a Location: header specifying the URI to redirect to, which
    that code doesn't do. Also, if you return a redirect code, you probably
    shouldn't include any page content (except perhaps a link to the
    redirected page). Note also that 301 is a permanent redirect, that is, it
    should be used when the URI has changed permanently (and might cause your
    browser to, e.g., update its bookmarks). You're probably not going to use
    it in normal operation.

    I'm not sure what the author was trying to do, but there is one quite
    common case where redirects are used with POST request. When the POST
    request does something that you are probably not going to want to repeat,
    but you want the resulting page to contain information that the user might
    want to refresh, it's common to have the POST return a 302 status (a
    temporary redirect).

    One example where this is useful is posting comments to a forum or blog.
    Usually, when the comment is posted, you want to return to the list of all
    the comments. But the user might refresh that list of comments, and you
    don't want their comment to be posted again when they refresh. So you
    would have the POST for the comment respond, not with a list of comments,
    but with a redirect _to_ the list of comments.
    I V, Aug 21, 2007
    #3
  4. Frank Millman wrote:
    > Hi all
    >
    > I have just started to dabble in writing my own web server.
    >
    > I googled for 'python web server', and this is the first hit -
    >
    > http://fragments.turtlemeat.com/pythonwebserver.php
    >

    [...]
    >
    > When responding to the POST data received, it sends a 301 response, no
    > headers, and then the html page.
    >
    > This works with a Firefox browser on Linux, but MSW IE6 displays 'the
    > page cannot be displayed'.
    >
    > According to the notes, "You don't have to know much about the HTTP
    > protocol at all. Except some basic that when the client request
    > something it is a "GET", and when the client sends something it is in
    > our case a POST. Some basic responce codes like 200 is OK for GET, and
    > 404 is file not found, 301 is OK for a Post."
    >
    > I googled for 'http response code 301', and found that it is actually
    > a redirection code. It seems that the notes are misleading.
    >


    Thanks to Chris and I V for your responses, which helped me to
    understand what is going on.

    I contacted the original author, Jon Berg, and (with his permission) I
    show below his reply.

    =============================================
    Hello,

    > I read your article on 'Making a simple web server in Python'. Thanks
    > a lot for this. It explains quite a few concepts in very few lines,
    > and is a good introduction to the subject.


    Great!

    > I have one question. When you respond to the POST message, you send a
    > 301 response, followed by the html page generated. This works using
    > Firefox on Linux, but MSW IE6 gives a 'Page cannot be displayed' error.
    >
    > If I change it to sending a 200 response, followed by a content-type
    > header, followed by the html page, it works correctly on both platforms.
    >
    > According to what I have read, 301 is actually a redirection response.
    > Can you explain how this is supposed to work, and why it does not work on IE6.


    You are correct that it is a redirect response.

    I think the script is a bit incomplete in this respect. Normally when
    you want to redirect a POST request it can be done by sending a
    redirect response, but it also requires the "Location:" header to
    work. I think this is what happens and maybe confusing to IE, but
    luckily Firefox is better at guessing what to do and just displays the
    content and gives up, right?

    It also seems to be more correct to return 302 or 304, when
    redirecting a POST request.

    There is also this design pattern to redirect after a POST that can be
    useful:
    http://en.wikipedia.org/wiki/Post/Redirect/Get

    Other that that if you don't want the redirecting stuff, the correct
    thing would then be to just return a 200 response code. And the
    content returned will be displayed in the browser.

    More about POST:
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5

    Have a nice evening.

    Jon Berg.
    =============================================

    This confirms exactly the responses from Chris and I V.

    I replied to Jon asking if he could update his page to avoid confusing
    other newbies, especially as it comes up first in a google search.

    His reply - "I can look into updating that page in the weekend. I have
    been a bit lazy doing anything with that site for a while."

    Just thought I would report all the above to tidy up any loose ends.

    Frank
    Frank Millman, Aug 22, 2007
    #4
    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. human
    Replies:
    1
    Views:
    327
    human
    Jul 6, 2003
  2. -
    Replies:
    2
    Views:
    386
    Alan Krueger
    Jul 29, 2005
  3. AAJ
    Replies:
    1
    Views:
    259
    Mark Fitzpatrick
    Oct 11, 2006
  4. Vaibhav Joshi
    Replies:
    0
    Views:
    878
    Vaibhav Joshi
    Aug 2, 2011
  5. Jerry C.
    Replies:
    8
    Views:
    206
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page