簡體   English   中英

Python socket.connect奇怪的行為

[英]Python socket.connect strange behaviour

我無法用Google搜尋,也無法理解這段程式碼的運作方式

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會做一些檢查並丟棄不正確的值,但是不提供有關該技巧的任何信息。

文檔中

socket.socket([family[, type[, proto]]])

使用socket.socket(socket.SOCK_DGRAM) ,即使這是一種類型,也可以將SOCK_DGRAM用作家族。 這將導致您遇到的奇怪問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM