Newbie question about a web server

F

Frank Millman

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
 
C

Chris Allen

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...
 
I

I V

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.
 
F

Frank Millman

Frank said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top