Python插座连接奇怪的行为

2024-09-27 07:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能谷歌它,也不能理解为什么这个代码的工作方式

Python 3.5.2 (default, Sep 28 2016, 18:08:09) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.SOCK_DGRAM)
>>> s.connect(('127.0.0.1', 33000))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ConnectionRefusedError: [Errno 61] Connection refused
>>> s.family
<AddressFamily.AF_INET: 2>
>>> 
>>>
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s.family
<AddressFamily.AF_INET: 2>
>>> s.connect(('127.0.0.1', 33000))
>>> 

编辑:

Python 3.5.2 (default, Sep 28 2016, 18:08:09) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s2 = socket.socket(socket.SOCK_DGRAM)
KeyboardInterrupt
>>> import socket
>>> s = socket.socket(socket.SOCK_DGRAM)
>>> s.connect(('127.0.0.1', 33000))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ConnectionRefusedError: [Errno 61] Connection refused
>>> s.family
<AddressFamily.AF_INET: 2>
>>> s.type
<SocketKind.SOCK_STREAM: 1>
>>>  

看起来这是TCP socket=)我猜Python做了一些检查并删除了不正确的值,但是没有提供任何关于这个技巧的信息。你知道吗


Tags: defaultappleconnectsocketfamilysepcompatiblellvm

热门问题