为什么 chrome 总是创建三个连接但只使用其中两个?
Why chrome always creates three connections but only use two of them?
Chrome 版本 70.0.3538.102
这是我的简单 http 服务器的代码(在 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()
我用 Microsoft Edge 对其进行了测试,它可以正常输出以下内容:
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
但是在我将浏览器切换到 Chrome 之后,发生了一些非常奇怪的事情。
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
似乎Chrome打开了三个连接,但只使用了两个。
最后一次连接没有向服务器发送任何消息。
您可以使用 except IndexError:
修复此错误
这是固定代码的样子:
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()
Chrome 版本 70.0.3538.102
这是我的简单 http 服务器的代码(在 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()
我用 Microsoft Edge 对其进行了测试,它可以正常输出以下内容:
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
但是在我将浏览器切换到 Chrome 之后,发生了一些非常奇怪的事情。
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
似乎Chrome打开了三个连接,但只使用了两个。 最后一次连接没有向服务器发送任何消息。
您可以使用 except IndexError:
修复此错误
这是固定代码的样子:
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()