Python如何访问socket()等OS API函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31190375/
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
How does Python access OS API functions such as socket()?
提问by Charlie
UPDATED Questions:
更新的问题:
Where does the socket object actually get created? I found this at line 4188 in socketmodule.c, but it looks like it's called sock_new not socket?
static PyObject *sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Is there some convention for figuring out where a module like socketmodule.c is imported? In other words, when I see a "from _socket import *" who do I know what that's importing (without searching the whole repository)?
套接字对象实际上是在哪里创建的?我在socketmodule.c 的第 4188 行找到了这个,但它看起来像是 sock_new 而不是 socket?
static PyObject *sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
是否有一些约定来确定像 socketmodule.c 这样的模块是从哪里导入的?换句话说,当我看到“from _socket import *”时,我知道它在导入什么(无需搜索整个存储库)?
ORIGINAL:
原来的:
sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
I'm trying to understand how this code works specifically how/where Python actually makes the OS function call to socket():
我试图了解这段代码是如何工作的,具体是 Python 如何/在何处实际使 OS 函数调用 socket():
class _socketobject(object):
__doc__ = _realsocket.__doc__
__slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
if _sock is None:
_sock = _realsocket(family, type, proto)
self._sock = _sock
for method in _delegate_methods:
setattr(self, method, getattr(_sock, method))
When I look up BSD sockets on Wikipedia I see this example which makes sense because the socket function is defined under types.h. In the above I see a call to realsocket which look like an OS function call, but I don't realsocket defined anywhere (I don't see anything about sockets at all in the Python27/include headers).
当我在 Wikipedia 上查找 BSD 套接字时,我看到这个示例很有意义,因为套接字函数是在 types.h 下定义的。在上面,我看到一个对 realsocket 的调用,它看起来像一个 OS 函数调用,但我没有在任何地方定义 realsocket(我在 Python27/include 头文件中根本看不到任何关于套接字的内容)。
/* Server code in C */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
struct sockaddr_in stSockAddr;
int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
采纳答案by CristiFati
It has everything to do with the 1stand the 2ndlines from socket.py:
它拥有一切做的1日和2个从行socket.py:
import _socket
from _socket import *
If you start Pythonand run the following code:
如果您启动Python并运行以下代码:
import socket
print dir(socket)
print dir(socket._socket)
you'll notice that socketonly exports a few extra things compared to socket._socket
.
你会发现,插座只出口了一些额外的东西相比socket._socket
。
Now, what is socket._socket
? It is a Pythondynamic module (meaning that it can be used just like any other python module), but it's written in C(so after compilation it has an OSspecific native form: .sounder Nixand .dll(.pyd) under Win). Its location is in the python lib folder (where socket.pyis also located): lib-dynload/_socket*.so.
现在,什么是socket._socket
?它是一个Python动态模块(意味着它可以像任何其他 Python 模块一样使用),但它是用C编写的(因此编译后它具有特定于操作系统的本机形式:Nix下的.so和.dll( .pyd) 下的赢)。它的位置在 python lib 文件夹中(socket.py也位于其中):lib-dynload/_socket*.so。
You can see where modules are located by printing them (in the same console where you ran the above code you could type):
您可以通过打印来查看模块所在的位置(在您可以键入的上述代码的同一控制台中):
print socket
print socket._socket
If you're more interested, its source code is located in the Pythonsource tarball in ${PYTHON_SRC_DIR}/Modules/socketmodule.c(it also has a header file). In those files, the wrapper functions (that are visible from Python) are defined and they call the native functions (e.g. socketfrom /usr/include/sys/socket.h).
如果您更感兴趣,它的源代码位于${PYTHON_SRC_DIR}/Modules/socketmodule.c(它还有一个头文件)中的Python源 tarball 中。在这些文件中,定义了包装函数(在Python中可见)并且它们调用本机函数(例如来自/usr/include/sys/socket.h 的套接字)。