为什么我的流套接字一次仅对来自浏览器的一个连接进行排队?
我将套接字绑定到 localhost:8888,以创建一个简单的服务器。
这是我用来创建服务器的简单 python 脚本。
import os
import socket
import fcntl
tuples = socket.getaddrinfo('localhost', 8888, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
res = tuples[0]
af, socktype, proto, canonname, sockaddr = res
sock = socket.socket(af, socktype, proto)
flags = fcntl.fcntl(sock.fileno(), fcntl.F_GETFD)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(sock.fileno(), fcntl.F_SETFD, flags)
sock.setblocking(0)
sock.bind(sockaddr)
sock.listen(128)
- 现在我使用交互式 shell 打开此脚本。
- 然后我转到浏览器,并访问 http://localhost:8888,它应该排队到我的服务器的连接。
- 最后一步我又重复了几次 每次在新选项卡中。
此时,我预计会有许多连接在我的服务器上排队。
为了检查这一点,我进入交互式 shell 并输入:
sock.accept()
这在第一次尝试时有效,但在后续尝试中有效尝试我得到 Erno 11 (EAGAIN),它告诉我没有其他连接在排队。但是,在我的浏览器中,我打开的每个选项卡上仍然显示小“连接”微调器!我已经在 Chrome 和 Firefox 中尝试过了。
我编写了自己的客户端,连接到 localhost:8888,并且能够毫无问题地对连接进行排队。这是怎么回事?谁能检查一下这个实验是否可以在自己的浏览器上验证?
为了方便起见,这是我的客户:
import os
import socket
import fcntl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 8888))
s.recv(1000)
I am binding a socket to localhost:8888, in order to make a simple server.
Here is the simple python script that I used to create the server.
import os
import socket
import fcntl
tuples = socket.getaddrinfo('localhost', 8888, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)
res = tuples[0]
af, socktype, proto, canonname, sockaddr = res
sock = socket.socket(af, socktype, proto)
flags = fcntl.fcntl(sock.fileno(), fcntl.F_GETFD)
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(sock.fileno(), fcntl.F_SETFD, flags)
sock.setblocking(0)
sock.bind(sockaddr)
sock.listen(128)
- Now I open this script using an interactive shell.
- Then I go to my browser, and visit http://localhost:8888, which should queue up a connection to my server.
- I repeat the last step a few more times,
each time inside a new tab.
At this point, I expect to have many connections queued up for my server.
To check this, I go into my interactive shell and I type:
sock.accept()
This works on the first try, but on subsequent tries I get Erno 11 (EAGAIN), which tells me that no other connections are queued up. However, in my browser, the little "Connecting" spinner is still going on each tab I opened! I have tried this in both Chrome and Firefox.
I my wrote my own client that connects to localhost:8888, and was able to queue up connections with no problems. What is going on here?? Can anyone please check if this experiment can be verified on their own browsers?
For convenience, here is my client:
import os
import socket
import fcntl
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 8888))
s.recv(1000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是浏览器方面的优化,试图重用相同的连接 - 请参阅 http://en .wikipedia.org/wiki/HTTP_persistent_connection。
如果您想确定,请使用 wireshark 嗅探流量,尽管您的基于本地主机的地址可能需要一些奇特的设置。
It is likely an optimisation on the side of your browser, trying to reuse the same connection - see http://en.wikipedia.org/wiki/HTTP_persistent_connection.
If you want to make sure, sniff the traffic using wireshark, although your localhost based address may require some fancy setup.