简单python服务器的问题。插座.接受()接受输入/favicon.ico公司即使在我(应该)关闭s

2024-04-20 13:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉堆栈溢出和套接字编程。提前感谢您的帮助!在

背景知识:我正在尝试实现一个简单的python服务器。我试图通过TCP进行连接,并试图从请求中返回一些已解析的文本(我试图发回文本变量“message”)。在

但是,似乎即使在我关闭连接之后,服务器端套接字也接受一些随机输入,称为“/图标文件“我不知道这是从哪里来的。这个循环接受“/图标文件“在返回到正在等待连接的状态之前。在

有人知道发生了什么事吗? 这是我的代码:

#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'

------------------------------------------------------

这是我的客户端请求:http://192.xxx.56.101:10016/sophie.JPG(堆栈溢出使我x出IP)

和我的客户端输出,似乎返回得很好

^{pr2}$

------------------------------------------------------

<我的服务器端(打印语句):

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...

——

我只希望返回这四行,因为连接关闭后,服务器应该返回到它的waiting状态。然而,它仍在接受一些名为/图标文件并在循环返回到等待状态之前再运行几次。在

有人知道这里发生了什么吗?在

谢谢!在

------------------------------------------------------

更新:

好的,我添加了您建议的行,我看到浏览器正在发送这些额外的请求,并且它们(根据您的说法)正在排队。在

除此之外,我还改了行:

serverSocket.listen(0)

serverSocket.listen(1)

然后我的代码按我希望的那样运行。)其实,我现在又试了一次,结果没有按预期运行。/favicon.ico公司请求仍在发送中)

我想我还有几个问题要问:

  1. 为什么浏览器对/favicon.ico公司当我没有要求它时(使用原始代码serverSocket(0)?

  2. 既然我已经允许服务器监听多个套接字连接,为什么假连接请求(/favicon.ico公司消失了吗?

谢谢,我也会阅读syn曲奇。在


Tags: to服务器youmessagelinesocketthis图标
2条回答

谢天谢地,您的服务器工作正常!在

调用后,尝试将此添加到代码中服务器套接字.accept():print addr。在

事情是这样的:

启动循环,然后调用accept()。您正在等待10016端口上的新连接。您接收该连接,提供响应,然后关闭该连接。在

然后再次循环-这样就可以接受另一个套接字连接了。这次,是为了/图标文件. 在

addr变量告诉您每个新的套接字连接(对于食品.jpg,和图标文件)在另一个端口上发生-这就是accept()的作用。在

因为您的代码一次只能处理一个连接,所以浏览器的favicon.ico公司排队。也就是说,浏览器已经请求连接到您的服务器以获取favicon,但是您的服务器尚未接受该连接。在

现在,理论上,您不应该接受任何积压的连接。但有一个陷阱!{如果你的内核}是启用TCP的话。(你怎么知道的?好吧,我用C语言做了一系列的网络工作,这对我很有帮助;Python抽象了许多这些细节。)

希望有帮助!在

你在用Chrome吗:server socket receives 2 http requests when I send from chrome and receives one when I send from firefox?在

I had a similar issue with my node server. It is due to the following bug in Chrome. In summary, Chrome is sending a request for a favicon on every request. As, is likely, you aren't sending a favicon back, it requests one after every legitimate request.

Firefox, and most other browsers, also send out a request for a favicon when they first connect, but cache the result i.e. if there isn't a favicon returned first time, they don't keep trying - which is why you're only seeing a single request from Firefox. It seems Chrome is unfortunately a little too persistent with its favicon requestiness.

相关问题 更多 >