• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Sample program performing domain name lookups and showing off EasyDialogs,
2Res and Dlg in the process"""
3
4import EasyDialogs
5from Carbon import Res
6from Carbon import Dlg
7import socket
8import string
9import macresource
10#
11# Definitions for our resources
12ID_MAIN=512
13
14ITEM_LOOKUP_ENTRY=1
15ITEM_RESULT=2
16ITEM_LOOKUP_BUTTON=3
17ITEM_QUIT_BUTTON=4
18
19def main():
20    """Main routine: open resource file, call dialog handler"""
21    macresource.need("DLOG", ID_MAIN, "dnslookup-1.rsrc")
22    do_dialog()
23
24def 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
38def 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
55main()
56