Server setup and browser checking
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class customHTTPServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('<HTML><body>Get!</body></HTML>')
return
server = HTTPServer(('',8080),customHTTPServer)
print 'server started at port 8080'
server.serve_forever()
Now when I go to http://localhost:8080 with my browser I can see the expected Get!.
Unexpected observations
Test 1:
Now I tested using different status codes in my server. I tried these status codes: 301, 302, 400, 402, 403, 404, 405, 406, 407, 408, 418, 500, 501, 502, 504, 505 and 511.
(Q1) Strangely when using different status codes(except for 407) I get the same behavior as I was using status code 200. Why is that?
Test 2
Secondly I replaced self.wfile.write('<HTML><body>Get!</body></HTML>')
with self.wfile.write('') for all the different status codes I tested in the 1st test. When I used status code 403, 404, 500, 501, 502, 504 or 505 I now got a specific browser message on the browser (403 Forbidden, 404 not found, …).
(Q2) Why do I only receive specific browser messages using these status codes but while not using the other ones?
Summary
I can distinquish 3 types of behavior:
- Using status code
301,302,400,402,405,406,408,418or511has
no difference with using status code200regardless if I am sending an empty or non-empty string. - Using status code
403,404,500,501,502,504or505will create a specific browser message on the browser but only when an empty string is
sent. - Using status code
407returns a specific browser message(showing a323error) regardless if I am sending an empty or non-empty string.
(A1) When you send error codes but return a body anyways, the browser will assume it is a custom error page sent by your webserver. This is the way that for example stackoverflow gives you a custom page for requests of unknown documents, instead of the default browserpage for 404.
(A2) Check the meaning of the statuscodes in which no browser message is displayed and ask yourself if it’s sensible to show a browser message when they are returned. Also, most likely, it will differ from browser to browser when messages are displayed or not.