python AF_INET 和 PF_INET 常量有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2549461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:54:20  来源:igfitidea点击:

What is the difference between AF_INET and PF_INET constants?

pythoncunixsockets

提问by Denilson Sá Maia

Looking at examples about socket programming, we can see that some people use AF_INETwhile others use PF_INET. In addition, sometimes both of them are used at the same example. The question is: Is there any difference between them? Which one should we use?

查看有关套接字编程的示例,我们可以看到有些人AF_INET使用PF_INET. 此外,有时在同一个示例中同时使用它们。问题是:它们之间有什么区别吗?我们应该使用哪一种?

If you can answer that, another question would be... Why there are these two similar (but equal) constants?

如果你能回答这个问题,另一个问题是......为什么有这两个相似(但相等)的常量?



What I've discovered, so far:

到目前为止,我发现了什么:

The socketmanpage

socket手册页

In (Unix) socket programming, we have the socket()function that receives the following parameters:

在(Unix)套接字编程中,我们有socket()接收以下参数的函数:

int socket(int domain, int type, int protocol);

The manpage says:

手册页说:

The domainargument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>.

所述domain参数指定的通信区域; 这将选择将用于通信的协议族。这些系列在 <sys/socket.h> 中定义。

And the manpage cites AF_INETas well as some other AF_constants for the domainparameter. Also, at the NOTESsection of the same manpage, we can read:

手册页引用了参数AF_INET以及其他一些AF_常量domain。此外,在NOTES同一手册页的部分,我们可以阅读:

The manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, etc., while AF_UNIX etc. are used for address families. However, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere.

在 4.x BSD 下用于协议族的清单常量是 PF_UNIX、PF_INET 等,而 AF_UNIX 等用于地址族。但是,BSD 手册页已经承诺:“协议族通常与地址族相同”,随后的标准到处都使用 AF_*。

The C headers

C 头文件

The sys/socket.hdoes not actually define those constants, but instead includes bits/socket.h. This file defines around 38 AF_constants and 38 PF_constants like this:

sys/socket.h实际没有定义这些常量,而是包括bits/socket.h。该文件定义了大约 38 个AF_常量和 38 个PF_常量,如下所示:

#define PF_INET     2   /* IP protocol family.  */
#define AF_INET     PF_INET

Python

Python

The Python socket moduleis very similar to the C API. However, there are many AF_constants but only one PF_constant (PF_PACKET). Thus, in Python we have no choice but use AF_INET.

Python的插座模块是非常相似的C API。然而,有很多AF_常量,但只有一个PF_常量(PF_PACKET)。因此,在 Python 中我们别无选择,只能使用AF_INET.

I think this decision to include only the AF_constants follows one of the guiding principles: "There should be one-- and preferably only one --obvious way to do it." (The Zen of Python)

我认为这个只包含AF_常量的决定遵循了一个指导原则:“应该有一种——最好只有一种——明显的方法来做到这一点。” (Python之禅)

Other info

其他信息

This forum postlinks to this old message, which contains some historical information.

此论坛帖子链接到此旧消息,其中包含一些历史信息。

采纳答案by Michael Burr

I think the Wikipedia notes on thissum it up pretty well:

我认为维基百科对此的注释总结得很好:

The original design concept of the socket interface distinguished between protocol types (families) and the specific address types that each may use. It was envisioned that a protocol family may have several address types. Address types were defined by additional symbolic constants, using the prefix AF_instead of PF_. The AF_-identifiers are intended for all data structures that specifically deal with the address type and not the protocol family. However, this concept of separation of protocol and address type has not found implementation support and the AF_-constants were simply defined by the corresponding protocol identifier, rendering the distinction between AF_versus PF_constants a technical argument of no significant practical consequence. Indeed, much confusion exists in the proper usage of both forms.

套接字接口的原始设计概念区分了协议类型(族)和每个可能使用的特定地址类型。设想一个协议族可能有几种地址类型。地址类型由附加符号常量定义,使用前缀AF_而不是PF_. 该AF_-identifiers旨在为专门与地址类型,而不是协议系列处理所有的数据结构。然而,这种协议和地址类型分离的概念并没有得到实现支持,-AF_常量只是简单地由相应的协议标识符定义,呈现出AF_PF_常数 一个没有重大实际后果的技术论证。事实上,在正确使用这两种形式时存在很多混淆。

Even if someone came up with a reason to have a difference today, they'd have to come up with new identifiers or so much stuff would break...

即使今天有人想出一个与众不同的理由,他们也必须想出新的标识符,否则很多东西都会破坏......