在cygwin

时间:2016-12-07 13:22:17

标签: python sockets 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()

1 个答案:

答案 0 :(得分:1)

从2018年4月4日的快照

安装cygwin1.dll后,问题得以解决
  

https://cygwin.com/snapshots/

感谢Corinna Vinschen和Mark Geisert的后续行动,

  

https://cygwin.com/ml/cygwin/2018-04/msg00019.html

我通过发送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)后,我来到了您的帖子,

_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