1
1
import socket
2
2
3
-
4
- def get_named_services (first_port = 0 , last_port = 2 ** 15 + 2 ** 14 ):
5
- tmpl = '{0: >4}: {1}'
6
- for i in xrange (first_port , last_port ):
7
- try :
8
- service = socket .getservbyport (i )
9
- except socket .error :
10
- continue
11
- print tmpl .format (i , service )
12
-
13
-
14
3
def get_constants (prefix ):
15
4
return dict (
16
5
(getattr (socket , n ), n )
@@ -21,10 +10,7 @@ def get_constants(prefix):
21
10
# this example is more 'pythonic' for 2.7 and above (where dictionary
22
11
# comprehensions exist)
23
12
def get_constants_27 (prefix ):
24
- return {
25
- getattr (socket , n ):n for n in dir (socket )
26
- if n .startswith (prefix )
27
- }
13
+ return {getattr (socket , n ):n for n in dir (socket ) if n .startswith (prefix )}
28
14
29
15
30
16
def get_address_info (host , port ):
@@ -39,13 +25,3 @@ def get_address_info(host, port):
39
25
print 'canonical name: ' , nam
40
26
print 'socket address: ' , add
41
27
print
42
-
43
-
44
- def print_address_constants ():
45
- addrs = get_constants ('INADDR' )
46
- for val , name in addrs .items ():
47
- hexval = hex (val )
48
- packed = socket .inet_aton (hexval )
49
- addr = socket .inet_ntoa (packed )
50
- tmpl = '{0: >24}: {1: <10} {2: <16}'
51
- print tmpl .format (name , hexval , addr )
0 commit comments