Home | History | Annotate | Download | only in example1
      1 """Sample program performing domain name lookups and showing off EasyDialogs,
      2 Res and Dlg in the process"""
      3 
      4 import EasyDialogs
      5 from Carbon import Res
      6 from Carbon import Dlg
      7 import socket
      8 import string
      9 import macresource
     10 #
     11 # Definitions for our resources
     12 ID_MAIN=512
     13 
     14 ITEM_LOOKUP_ENTRY=1
     15 ITEM_RESULT=2
     16 ITEM_LOOKUP_BUTTON=3
     17 ITEM_QUIT_BUTTON=4
     18 
     19 def main():
     20     """Main routine: open resource file, call dialog handler"""
     21     macresource.need("DLOG", ID_MAIN, "dnslookup-1.rsrc")
     22     do_dialog()
     23 
     24 def do_dialog():
     25     """Post dialog and handle user interaction until quit"""
     26     my_dlg = Dlg.GetNewDialog(ID_MAIN, -1)
     27     while 1:
     28         n = Dlg.ModalDialog(None)
     29         if n == ITEM_LOOKUP_BUTTON:
     30             tp, h, rect = my_dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
     31             txt = Dlg.GetDialogItemText(h)
     32 
     33             tp, h, rect = my_dlg.GetDialogItem(ITEM_RESULT)
     34             Dlg.SetDialogItemText(h, dnslookup(txt))
     35         elif n == ITEM_QUIT_BUTTON:
     36             break
     37 
     38 def dnslookup(str):
     39     """ Perform DNS lookup on str.  If first character of digit is numeric,
     40         assume that str contains an IP address.  Otherwise, assume that str
     41         contains a hostname."""
     42     if str == '': str = ' '
     43     if str[0] in string.digits:
     44         try:
     45             value = socket.gethostbyaddr(str)[0]
     46         except:
     47             value = 'Lookup failed'
     48     else:
     49         try:
     50             value = socket.gethostbyname(str)
     51         except:
     52             value = 'Lookup failed'
     53     return value
     54 
     55 main()
     56