1
2
3 """Miscellaneous functions.
4
5 @var table: Translation table for normalizing strings.
6 @type table: C{str}
7 """
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import time
27 import socket
28 import urlparse
29
30
31 table = '________________________________________________0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz_____________________________________________________________________________________________________________________________________'
32
33
35 """Generate translation table.
36 """
37 tab = ''
38 for c in map(chr, xrange(256)):
39 tab += (c.isalnum() and c) or '_'
40
41 return tab
42
43
45 return time.mktime(time.gmtime())
46
47
49 """Get the hostname part of an URL.
50
51 @param url: A valid URL (must be preceded by scheme://).
52 @type url: C{str}
53
54 @return: Hostname corresponding to the URL or the empty string in case of
55 failure.
56 @rtype: C{str}
57 """
58 netloc = urlparse.urlparse(url)[1]
59 if netloc == '':
60 return ''
61
62 return netloc.split(':', 1)[0]
63
65 """Get the network addresses to which a given host resolves to.
66
67 @param host: Hostname we want to resolve.
68 @type host: C{str}
69
70 @return: Network addresses.
71 @rtype: C{tuple}
72 """
73 assert host != ''
74
75 try:
76 name, aliases, addrs = socket.gethostbyname_ex(host)
77 except socket.error:
78 return ()
79
80 return addrs
81
82
83 if __name__ == '__main__':
84 print "table = '%s'" % _gen_table()
85
86
87
88