socket.accept() 在 运行 cygwin 中的脚本时不起作用
socket.accept() is not working when running the script in cygwin
我试图在 cygwin 中 运行 一个基本的 python TCP 服务器。但是不知何故 socket.accept() 方法失败了。服务器似乎处于活动状态,因为在浏览器中输入地址 127.0.0.1:8080 时浏览器正在加载...
之后,我在正常 cmd.exe 中尝试了该脚本,它运行良好。有人对此有解释吗?
这是我的代码:
import socket
# create a socket object
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = '127.0.0.1'
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
msg='Thank you for connecting'+ "\r\n"
clientsocket.send(msg.encode('ascii'))
clientsocket.close()
安装 2018 年 4 月 4 日快照 cygwin1.dll 后问题解决,
感谢 Corinna Vinschen 和 Mark Geisert follow-up,
我通过发送 TCP 请求重现了这个问题,
$ { echo test; sleep 1; } | /cygdrive/c/util/nc/nc.exe -v -w 2 127.0.0.1 9999
HOSTNAME [127.0.0.1] 9999 (?) open
同时让您的 Python 代码监听,
$ python accept.py
Traceback (most recent call last):
File "accept.py", line 20, in <module>
clientsocket,addr = serversocket.accept()
File "/usr/lib/python2.7/socket.py", line 206, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 14] Bad address
不确定这是否相关,但我在 startxwin
中观察到一个错误(当 XWin 收到来自 x11 客户端(如 xterm)的连接请求时)后,我来到了您的 post,
_XSERVTransSocketUNIXAccept: accept() failed
并在sshd
中(如上向端口22发送测试请求后),
C:\cygwin64>c:\cygwin64\usr\sbin\sshd.exe -ddd
debug2: load_server_config: filename /etc/sshd_config
debug2: load_server_config: done config len = 209
debug2: parse_server_config: config /etc/sshd_config len 209
debug3: /etc/sshd_config:54 setting AuthorizedKeysFile .ssh/authorized_keys
debug3: /etc/sshd_config:126 setting Subsystem sftp /usr/sbin/sftp-server
debug1: sshd version OpenSSH_7.6, OpenSSL 1.0.2n 7 Dec 2017
debug1: private host key #0: ssh-rsa SHA256:XXXXXXX
debug1: private host key #1: ssh-dss SHA256:XXXXXXX
debug1: private host key #2: ecdsa-sha2-nistp256 SHA256:XXXXXXX
debug1: private host key #3: ssh-ed25519 SHA256:XXXXXXX
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-ddd'
debug2: fd 3 setting O_NONBLOCK
debug3: sock_set_v6only: set socket 3 IPV6_V6ONLY
debug1: Bind to port 22 on ::.
Server listening on :: port 22.
debug2: fd 4 setting O_NONBLOCK
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
accept: Bad address
一些人观察到类似的错误 Cygwin Bad Address 但他们的问题已通过 2014 年 Cygwin 快照或安装 32 位 Cygwin 解决。我想知道后者是否与 Windows、
中的 WOW64 错误有关
http://zachsaw.blogspot.ca/2010/11/wow64-bug-getthreadcontext-may-return.html
$ uname -a
CYGWIN_NT-6.1 XXXXXX 2.10.1(0.325/5/3) x86_64 Cygwin
我试图在 cygwin 中 运行 一个基本的 python TCP 服务器。但是不知何故 socket.accept() 方法失败了。服务器似乎处于活动状态,因为在浏览器中输入地址 127.0.0.1:8080 时浏览器正在加载...
之后,我在正常 cmd.exe 中尝试了该脚本,它运行良好。有人对此有解释吗?
这是我的代码:
import socket
# create a socket object
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = '127.0.0.1'
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
msg='Thank you for connecting'+ "\r\n"
clientsocket.send(msg.encode('ascii'))
clientsocket.close()
安装 2018 年 4 月 4 日快照 cygwin1.dll 后问题解决,
感谢 Corinna Vinschen 和 Mark Geisert follow-up,
我通过发送 TCP 请求重现了这个问题,
$ { echo test; sleep 1; } | /cygdrive/c/util/nc/nc.exe -v -w 2 127.0.0.1 9999
HOSTNAME [127.0.0.1] 9999 (?) open
同时让您的 Python 代码监听,
$ python accept.py
Traceback (most recent call last):
File "accept.py", line 20, in <module>
clientsocket,addr = serversocket.accept()
File "/usr/lib/python2.7/socket.py", line 206, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 14] Bad address
不确定这是否相关,但我在 startxwin
中观察到一个错误(当 XWin 收到来自 x11 客户端(如 xterm)的连接请求时)后,我来到了您的 post,
_XSERVTransSocketUNIXAccept: accept() failed
并在sshd
中(如上向端口22发送测试请求后),
C:\cygwin64>c:\cygwin64\usr\sbin\sshd.exe -ddd
debug2: load_server_config: filename /etc/sshd_config
debug2: load_server_config: done config len = 209
debug2: parse_server_config: config /etc/sshd_config len 209
debug3: /etc/sshd_config:54 setting AuthorizedKeysFile .ssh/authorized_keys
debug3: /etc/sshd_config:126 setting Subsystem sftp /usr/sbin/sftp-server
debug1: sshd version OpenSSH_7.6, OpenSSL 1.0.2n 7 Dec 2017
debug1: private host key #0: ssh-rsa SHA256:XXXXXXX
debug1: private host key #1: ssh-dss SHA256:XXXXXXX
debug1: private host key #2: ecdsa-sha2-nistp256 SHA256:XXXXXXX
debug1: private host key #3: ssh-ed25519 SHA256:XXXXXXX
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-ddd'
debug2: fd 3 setting O_NONBLOCK
debug3: sock_set_v6only: set socket 3 IPV6_V6ONLY
debug1: Bind to port 22 on ::.
Server listening on :: port 22.
debug2: fd 4 setting O_NONBLOCK
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
accept: Bad address
一些人观察到类似的错误 Cygwin Bad Address 但他们的问题已通过 2014 年 Cygwin 快照或安装 32 位 Cygwin 解决。我想知道后者是否与 Windows、
http://zachsaw.blogspot.ca/2010/11/wow64-bug-getthreadcontext-may-return.html
$ uname -a
CYGWIN_NT-6.1 XXXXXX 2.10.1(0.325/5/3) x86_64 Cygwin