Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbie question about a web server

Reply
Thread Tools

Newbie question about a web server

 
 
Frank Millman
Guest
Posts: n/a
 
      08-20-2007
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

 
Reply With Quote
 
 
 
 
Chris Allen
Guest
Posts: n/a
 
      08-20-2007
On Aug 20, 12:02 am, Frank Millman <(E-Mail Removed)> 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...



 
Reply With Quote
 
 
 
 
I V
Guest
Posts: n/a
 
      08-21-2007
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.
 
Reply With Quote
 
Frank Millman
Guest
Posts: n/a
 
      08-22-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/...c9.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
Newbie question on publishing to web server AAJ ASP .Net 1 10-11-2006 12:04 PM
Create a VD with required properties on a required Web Server (Windows Advanced Server 2000 has 3 Web Servers) Just D. ASP .Net 0 03-10-2006 06:02 AM
Server to server = Server client to server? - Java 2 07-29-2005 10:46 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments