Re: Newbie question about a web server



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

.



Relevant Pages

  • Re: Post with redirect?
    ... How can I force a POST but actually redirect the user as well ... and it can't be done with PHP. ... return $response; ... its a credit card entry form on a completely separate server. ...
    (comp.lang.php)
  • Re: Redirected request in HttpWebRequest does not maintain specified method!!
    ... XML-RPC client to expect to re-post the content after the redirect, ... redirected URL is just that--a response, ... An HTTP server response is just that: ... Otherwise, Steve, you need to extend the Blogger API to offer a GET ...
    (microsoft.public.dotnet.framework)
  • Re: Query: How do I design an ENTER page?
    ... >it to create a session variable reflecting their response. ... redirect them to the pages you ... Even if they bookmark it, ... it would need some form of protection ...
    (microsoft.public.frontpage.client)
  • Response.Redirect from SSL to non SSL with port drops port.
    ... When they post the data back, and the processing is done, it then tries to redirect them back to the non-SSL site. ... But the site is on a non-starndard port number. ... I place in the Response.Redirect() method the Fully qualified path to the URL and when ASP.NET makes the Response, the response drops the port number, therefore redirecting the user to a 404. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Redirected request in HttpWebRequest does not maintain specified method!!
    ... XML-RPC client to expect to re-post the content after the redirect, ... redirected URL is just that--a response, ... An HTTP server response is just that: ... Otherwise, Steve, you need to extend the Blogger API to offer a GET ...
    (microsoft.public.dotnet.languages.csharp)

Loading