Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python access OS API functions such as socket()?

Tags:

python

sockets

UPDATED Questions:

  1. 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)
    
  2. 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)?


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():

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).

  /* 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);
like image 607
Charlie Avatar asked Jul 02 '15 16:07

Charlie


People also ask

What is the Python socket API?

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC). The network can be a logical, local network to the computer, or one that's physically connected to an external network, with its own connections to other networks.

What is the socket module in Python?

The 'socket' module defines how server and client machines can communicate at hardware level using socket endpoints on top of the operating system. The 'socket' API supports both connection-oriented and connectionless network protocols.

Which method of the socket module allows a server socket to accept requests from a client socket from another host?

accept() − This will accept TCP client connection. The pair (conn, address) is the return value pair of this method. Here, conn is a new socket object used to send and receive data on the connection and address is the address bound to the socket.


1 Answers

It has everything to do with the 1st and the 2nd lines from socket.py:

import _socket
from _socket import *

If you start Python and run the following code:

import socket
print dir(socket)
print dir(socket._socket)

you'll notice that socket only exports a few extra things compared to socket._socket.

Now, what is socket._socket? It is a Python dynamic 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 OS specific native form: .so under Nix and .dll (.pyd) under Win). Its location is in the python lib folder (where socket.py is also located): 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 Python source 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. socket from /usr/include/sys/socket.h).

like image 79
CristiFati Avatar answered Sep 28 '22 23:09

CristiFati