Search code examples
pythongoogle-chromewebserver

Why chrome always creates three connections but only use two of them?


Chrome version 70.0.3538.102

Here's the code for my simple http-server (in python)

from socket import *
import sys

server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.bind(('localhost', 8080))
server_socket.listen(5)

print('Starting up http-server, serving ./')
print('Available on:')
print('  http://localhost:8080')
print('Hit CTRL-BREAK to stop the server')

while True:
    connection_socket, address = server_socket.accept()
    print()
    print('Connected by: ', address[0] + ':' + str(address[1]))

    try:
        message = connection_socket.recv(1024)
        filename = message.split()[1]
        print('Filename to get: ', filename[1:].decode())
        f = open(filename[1:], 'rb')
        output_data = f.read()
        connection_socket.send('HTTP/1.1 200 OK\r\n\r\n'.encode())
        connection_socket.send(output_data)
        connection_socket.send('\r\n'.encode())
        connection_socket.close()
    except IOError:
        connection_socket.send('HTTP/1.1 404 Not Found\r\n\r\n'.encode())
        connection_socket.send('404 Not Found'.encode())
        connection_socket.send('\r\n'.encode())
        connection_socket.close()

server_socket.close()
sys.exit()

I tested it with Microsoft Edge and it works fine with output below:

Starting up http-server, serving ./
Available on:
  http://localhost:8080
Hit CTRL-BREAK to stop the server

Connected by:  127.0.0.1:60998
Filename to get:  test.html

But after I switched my browser to Chrome something very strange happened.

Starting up http-server, serving ./
Available on:
  http://localhost:8080
Hit CTRL-BREAK to stop the server

Connected by:  127.0.0.1:61332
Filename to get:  test.html

Connected by:  127.0.0.1:61333
Filename to get:  favicon.ico

Connected by:  127.0.0.1:61335
Traceback (most recent call last):
  File "http-server.py", line 20, in <module>
    filename = message.split()[1]
IndexError: list index out of range

It seems that Chrome had opened three connections but only used two of them. The last connection didn't send any message to the server.


Solution

  • You can fix this error using except IndexError:
    This is how looks fixed code:

    from socket import *
    import sys
    
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.bind(('localhost', 8080))
    server_socket.listen(5)
    
    print('Starting up http-server, serving ./')
    print('Available on:')
    print('  http://localhost:8080')
    print('Hit CTRL-BREAK to stop the server')
    
    while True:
        connection_socket, address = server_socket.accept()
        print()
        print('Connected by: ', address[0] + ':' + str(address[1]))
    
        try:
            message = connection_socket.recv(1024)
            filename = message.split()[1]
            print('Filename to get: ', filename[1:].decode())
            f = open(filename[1:], 'rb')
            output_data = f.read()
            connection_socket.send('HTTP/1.1 200 OK\r\n\r\n'.encode())
            connection_socket.send(output_data)
            connection_socket.send('\r\n'.encode())
            connection_socket.close()
        except IOError:
            connection_socket.send('HTTP/1.1 404 Not Found\r\n\r\n'.encode())
            connection_socket.send('404 Not Found'.encode())
            connection_socket.send('\r\n'.encode())
            connection_socket.close()
        except IndexError:
            # what you want to do when request is invalid
    
    server_socket.close()
    sys.exit()