我不能google一下,并不能明白为什么这个代码工作它的工作原理Python的socket.connect奇怪的行为
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套接字=)我猜Python做了一些检查并丢弃了不正确的值,但没有提供任何有关该技巧的信息。
顺便说一句,python2也是如此 –