[Python-Dev] Getting socket information from socket objects

Pavel Pergamenshchik pp64 at cornell.edu
Fri Nov 14 12:29:02 EST 2003

It appears that the easiest way to retrieve family/type/protocol fields from socket objects is this:
def getsockinfo(sock):
    s = `sock._sock`
    sp = s[1:-1].split(",")[1:]
    g = {}
    d = {}
    for i in sp:
        exec i.strip() in g, d
    return (d["family"], d["type"], d["protocol"])
Wouldn't it be nice to have accessors for these fields? My particular use-case is Windows-specific (IO completion port proactor), so winsock API provides this, but I'd rather avoid that crud.
Also, exporting getsockaddrarg in socketmodule.c CAPI would be useful, although the only use I can think of is implementing Windows' ConnectEx (which I am doing)

More information about the Python-Dev mailing list