0

I've written a simple http web server using python but I've noticed that when I connect to it, the html page appears in the browser window but the indicator in the chrome tab continues to spin and the server receives empty strings. This continues until I click the 'X' to stop loading the page. Could someone please explain why this is happening and how to fix this. Also, if http headers are wrong or I'm missing important ones please tell me. I found it very difficult to find information on http headers and commands.

You find the code here.

Link to image of network tab

Console output:

Socket created
Bound socket
Socket now listening
Connected with 127.0.0.1:55146
Connected with 127.0.0.1:55147Received data: GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0 .8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6



Parsing GET command
Client requested directory /index.html with HTTP version 1.1
html
/index.html

Reply headers:


HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: Max's Python Web Server/1.0
Cache-Control: max-age=600, public


Connected with 127.0.0.1:55148
Received data:
Received data:
Received data:
Received data:

icedvariables
  • 99
  • 1
  • 9
  • 1
    All indents are missed in your sorces, isn't it? We can't read python code without indents) – vp_arth Mar 30 '14 at 16:51
  • What does your browser's dev tools' network tab say? – lmc Mar 30 '14 at 17:06
  • @vp_arth Sorry, I didn't realise that indents are removed on the website I used previously. I've added a new link to a different website which keeps indents and has syntax highlighting. – icedvariables Mar 31 '14 at 15:06
  • @lmc I've added a link to and image of my network tab. Though I'm not hugely experienced with browsers and networking, I don't see anything that looks unusual compared to other sites. – icedvariables Mar 31 '14 at 15:13

1 Answers1

1

Your fault is how you think about sockets:

socket.recv will wait forever for data from clients

You don't need a loop here.
However, your requests will be limited by recv param.
But if you want to allow any size request,
you should detect the end of data by HTTP specification.

For example, if you wait headers only, double linefeed will mean they ends.
And size of body(for POST method for example) should be passed with Content-length header as I know.

You issue is same as in this question: link
And google for HTTP Specifications, if you want to make correct HTTP server.

Community
  • 1
  • 1
vp_arth
  • 14,461
  • 4
  • 37
  • 66