admin管理员组文章数量:1394979
I am writing a Python 3-based server/client program using a Unix-domain socket. I am trying to build the server such that only a single client can connect to the server and subsequent connect() calls should return connection refused error until the connected client disconnects.
The client behaves exactly as expected when netcat (nc
) is used as the server. But for my Python server code even accepts only a single accept()
call on a socket that has been configured with listen(0)
. Multiple clients successfully connect without error.
Behaviour with netcat (nc
):
$ nc -lU /tmp/uds0 &
[1] 387992
$
$ nc -U /tmp/uds0 &
[2] 388009
$
$ nc -U /tmp/uds0
Ncat: Connection refused.
Unexpected behaviour with python code:
$ cat uds-server.py
#! /usr/bin/python3
import socket
import os
import sys
SOCKET_PATH = "/tmp/uds0"
# Create the socket
server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Bind the socket to the file path
server_socket.bind(SOCKET_PATH)
# Listen - handle number of queued connections
server_socket.listen(0)
# accept a single client and proceed
(client_socket, client_address) = server_socket.accept()
print("New client connected, waiting for messages")
msg = client_socket.recv(1023)
print(msg.decode())
$
$
$ ./uds-server.py &
[5] 388954
$
$ nc -U /tmp/uds0 &
[6] 389017
$ New client connected, waiting for messages
$
$ nc -U /tmp/uds0
How can I make the Python server behave the same way as netcat?
本文标签:
版权声明:本文标题:python - Second client connect() on unix domain socket succeeds even when listen() set to zero and server only accepts a single 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107413a2591129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论