我无法谷歌,并且无法理解为什么这段代码的工作方式
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会做一些检查并丢弃不正确的值但不提供有关该技巧的任何信息。
答案 0 :(得分:0)
socket.socket([family[, type[, proto]]])
socket.socket(socket.SOCK_DGRAM)
使用SOCK_DGRAM
作为家人即使这是一种类型。这会引起你所见过的奇怪问题。