[code.view]

[top] / python / PyMOTW / socket / socket_getprotobyname.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Lookup the constant for a named protocol.
     """
     #end_pymotw_header
     
     import socket
     
     def get_constants(prefix):
         """Create a dictionary mapping socket module constants to their names."""
         return dict( (getattr(socket, n), n)
                      for n in dir(socket)
                      if n.startswith(prefix)
                      )
     
     protocols = get_constants('IPPROTO_')
     
     for name in [ 'icmp', 'udp', 'tcp' ]:
         proto_num = socket.getprotobyname(name)
         const_name = protocols[proto_num]
         print '%4s -> %2d (socket.%-12s = %2d)' % \
             (name, proto_num, const_name, getattr(socket, const_name))
     

[top] / python / PyMOTW / socket / socket_getprotobyname.py

contact | logmethods.com