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
What is the difference between AF_INET and PF_INET constants?
提问by Denilson Sá Maia
Looking at examples about socket programming, we can see that some people use AF_INET
while 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 socket
manpage
该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
domain
argument 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_INET
as well as some other AF_
constants for the domain
parameter. Also, at the NOTES
section 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.h
does 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 ofPF_
. TheAF_
-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 theAF_
-constants were simply defined by the corresponding protocol identifier, rendering the distinction betweenAF_
versusPF_
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...
即使今天有人想出一个与众不同的理由,他们也必须想出新的标识符,否则很多东西都会破坏......