简单python服务器出现问题。socket.accept()接受输入/favicon.ico,即使在我(假定)关闭套接字之后
我不熟悉堆栈溢出和套接字编程。提前感谢您的帮助 一点背景:我正在尝试实现一个简单的python服务器。我试图通过TCP进行连接,只是试图从请求返回一些经过解析的文本(我试图发回文本变量“message”) 然而,似乎即使在我关闭连接之后,服务器端套接字也会接受一些名为“/favicon.ico”的随机输入,我不确定这是从哪里来的。此循环在返回到等待连接的状态之前接受“/favicon.ico”几次 有人知道发生了什么事吗? 这是我的代码:简单python服务器出现问题。socket.accept()接受输入/favicon.ico,即使在我(假定)关闭套接字之后,python,sockets,Python,Sockets,我不熟悉堆栈溢出和套接字编程。提前感谢您的帮助 一点背景:我正在尝试实现一个简单的python服务器。我试图通过TCP进行连接,只是试图从请求返回一些经过解析的文本(我试图发回文本变量“message”) 然而,似乎即使在我关闭连接之后,服务器端套接字也会接受一些名为“/favicon.ico”的随机输入,我不确定这是从哪里来的。此循环在返回到等待连接的状态之前接受“/favicon.ico”几次 有人知道发生了什么事吗? 这是我的代码: #import socket module from s
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 10016
serverName = '192.168.56.101'
serverSocket.bind((serverName,serverPort))
serverSocket.listen(0)
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
message = connectionSocket.recv(4096)
filename = message.split()[1]
#f = open(filename[1:])
print filename
connectionSocket.send(message)
connectionSocket.close()
print '\nYou got to this line\n'
name@name-VirtualBox:~/Documents/python_scripts$ python server2.py
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
----------------------------------------------------------------
这是我的客户端请求:(堆栈溢出使我x出了IP)
和我的客户端输出,似乎返回正常:
GET /sophie.JPG HTTP/1.1
Host: 192.168.56.101:10016
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
----------------------------------------------------------------
这是我的服务器端输出(打印语句):
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 10016
serverName = '192.168.56.101'
serverSocket.bind((serverName,serverPort))
serverSocket.listen(0)
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
message = connectionSocket.recv(4096)
filename = message.split()[1]
#f = open(filename[1:])
print filename
connectionSocket.send(message)
connectionSocket.close()
print '\nYou got to this line\n'
name@name-VirtualBox:~/Documents/python_scripts$ python server2.py
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
---------------------------------------------------------------
我希望输出仅仅是前四行:
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 10016
serverName = '192.168.56.101'
serverSocket.bind((serverName,serverPort))
serverSocket.listen(0)
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
message = connectionSocket.recv(4096)
filename = message.split()[1]
#f = open(filename[1:])
print filename
connectionSocket.send(message)
connectionSocket.close()
print '\nYou got to this line\n'
name@name-VirtualBox:~/Documents/python_scripts$ python server2.py
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
----
我只希望返回这四行,因为服务器应该在连接关闭后返回等待状态。但是,它仍在接受一些名为/favicon.ico的输入,并在返回等待状态之前再运行循环几次
有人知道这里发生了什么吗
谢谢
---------------------------------------
更新:
#import socket module
from socket import *
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a sever socket
serverPort = 10016
serverName = '192.168.56.101'
serverSocket.bind((serverName,serverPort))
serverSocket.listen(0)
while True:
#Establish the connection
print 'Ready to serve...'
connectionSocket, addr = serverSocket.accept()
message = connectionSocket.recv(4096)
filename = message.split()[1]
#f = open(filename[1:])
print filename
connectionSocket.send(message)
connectionSocket.close()
print '\nYou got to this line\n'
name@name-VirtualBox:~/Documents/python_scripts$ python server2.py
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
/favicon.ico
You got to this line
Ready to serve...
Ready to serve...
/sophie.JPG
You got to this line
Ready to serve...
好的,我添加了您建议的行,我看到浏览器正在发送这些额外的请求,并且它们(根据您)正在排队
除此之外,我还更改了行:
serverSocket.listen(0)
到
然后,我的代码按我希望的方式运行。(事实上,我现在已再次尝试,但它没有按预期运行。仍在发送/favicon.ico请求)
我想我有几个关于正在发生的事情的后续问题:
谢谢,我也会阅读syn cookies。谢天谢地,您的服务器工作正常 在调用serversocket.accept()之后,尝试将其添加到代码中:
print addr
发生的事情是这样的:
启动循环并调用accept()。您正在等待新连接到达端口10016。您接收该连接,提供响应,然后关闭该连接
然后再次循环,这样就可以接受另一个套接字连接了
addr
变量告诉您每个新的套接字连接(对于foo.jpg和favicon.ico)都发生在不同的端口上,这就是accept()的作用
由于您的代码一次只能处理一个连接,因此浏览器对favicon.ico的请求进入队列。也就是说,浏览器已请求连接到您的服务器以获取favicon,但您的服务器尚未接受该连接
现在,从理论上讲,您不应该接受任何积压的连接。但是有一个问题!事实证明,如果在您的内核上启用了TCP syn Cookie,(您怎么知道这一点呢?我用C语言完成了一系列联网工作,这很有帮助;Python抽象掉了许多这些细节。)
希望有帮助!您是否正在使用Chrome浏览器 我的节点服务器也有类似的问题。这是由于Chrome中的以下错误造成的。总之,Chrome在每个请求中都会发送一个favicon请求。很可能,您没有发回favicon,它会在每个合法请求之后请求一个favicon Firefox和大多数其他浏览器在第一次连接时也会发送一个favicon请求,但会缓存结果,即如果没有第一次返回favicon,他们不会继续尝试-这就是为什么你只看到来自Firefox的一个请求。不幸的是,Chrome的favicon请求过于持久
感谢您的回复。我无法格式化此回复,因此我将更新底部的提交内容。我明白了。我正在使用firefox,但同样的情况正在发生。我将阅读您发送给我的链接。谢谢!