Newbie question about a web server

Frank Millman frank at chagford.com
Wed Aug 22 04:44:55 EDT 2007


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




More information about the Python-list mailing list