拒绝连接

时间:2012-09-12 19:26:11

标签: python sockets

导致以下问题的原因是什么:

>>> 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

2 个答案:

答案 0 :(得分:5)

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

请参阅http://docs.python.org/library/socket.html#example

上的套接字示例

答案 1 :(得分:1)