Package Halberd :: Module util
[hide private]
[frames] | no frames]

Source Code for Module Halberd.util

 1  # -*- coding: iso-8859-1 -*- 
 2   
 3  """Miscellaneous functions. 
 4   
 5  @var table: Translation table for normalizing strings. 
 6  @type table: C{str} 
 7  """ 
 8   
 9  # Copyright (C) 2004, 2005, 2006, 2010  Juan M. Bello Rivas <jmbr@superadditive.com> 
10  # 
11  # This program is free software; you can redistribute it and/or modify 
12  # it under the terms of the GNU General Public License as published by 
13  # the Free Software Foundation; either version 2 of the License, or 
14  # (at your option) any later version. 
15  # 
16  # This program is distributed in the hope that it will be useful, 
17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
19  # GNU General Public License for more details. 
20  # 
21  # You should have received a copy of the GNU General Public License 
22  # along with this program; if not, write to the Free Software 
23  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
24   
25   
26  import time 
27  import socket 
28  import urlparse 
29   
30   
31  table = '________________________________________________0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz_____________________________________________________________________________________________________________________________________' 
32   
33   
34 -def _gen_table():
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
44 -def utctime():
45 return time.mktime(time.gmtime())
46 47
48 -def hostname(url):
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
64 -def addresses(host):
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 # vim: ts=4 sw=4 et 88