socket module

Skip Montanaro skip at pobox.com
Mon Jul 9 10:43:39 EDT 2001


    Martin> Is there ANY way to use the socket module from a Python module
    Martin> written in C?

Sure.  Check the C API manual:

    http://www.python.org/doc/api/api.html

especially the sections on importing modules and the object protocol section
(for calling callable objects).

Basically, you'll do just what you'd do from Python:

    import socket
    sock = socket.socket(...)
    sock.connect(...)

Something vaguely like:

    PyObject *sockmodule, *sockfunc, *socket;
    sockmodule = PyImport_Import("socket");
    sockfunc = PyDict_GetItemString(PyModule_GetDict(sockmodule), "socket");
    PyCall_Object(sockfunc, ...);

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list