0

导致以下问题的原因:

>>> s = socket(AF_INET, SOCK_STREAM)
>>> s
<socket._socketobject object at 0x104a7a670>
>>> gethostname()
'MacBook-Air-user.local'
>>> s.connect((gethostname(), 4444))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
>>> s.connect((gethostname(), 4444))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
>>> s.bind((gethostname(), 4446))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument

在系统上:

MacBook-Air-user:source user$ uname -a
Darwin MacBook-Air-user.local 11.4.0 Darwin Kernel Version 11.4.0: Mon Apr  9 19:32:15 PDT 2012; root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64
4

2 回答 2

5

您正在尝试在端口 4444 上打开与计算机的网络连接,但该端口上没有服务器侦听

请参阅http://docs.python.org/library/socket.html#example上的套接字示例

于 2012-09-12T19:33:06.580 回答
1

应该是socket.gethostname()

于 2013-11-09T16:23:44.287 回答