network protocols

Christian Heimes lists at cheimes.de
Wed Jun 13 10:56:12 EDT 2012


Am 13.06.2012 13:41, schrieb Tarek Ziadé:
> Hey
> 
> I was surprised not to find any way to list all protocol names listed in
> /etc/protocols in Python
> 
> We have
> 
> socket.getprotobyname(NAME)
> 
> But there's no way to get the list of names
> 
> Any ideas if this is available in the stdlib somehwere ?

No, I can't find any reference to the relevant NSS APIs in the Python
code. You can easily roll your own with ctypes:


from ctypes import *
libc = CDLL("libc.so.6")

class protoent(Structure):
    _fields_ = [("p_name", c_char_p),
                ("p_aliases", POINTER(c_char_p)),
                ("p_proto", c_int)]

libc.getprotoent.restype = POINTER(protoent)

# open database
libc.setprotoent(0)

while True:
    pr = libc.getprotoent()
    if not pr:
        break
    r = pr[0]
    print r.p_name, r.p_proto

# close database
libc.endprotoent()




More information about the Python-list mailing list