summaryrefslogtreecommitdiffstats
path: root/Mac/Demo/example1/dnslookup-1.py
blob: 2fb8dc4405d811e6c1471776cd41ed6a43ee44a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Sample program performing domain name lookups and showing off EasyDialogs,
Res and Dlg in the process"""

import EasyDialogs
from Carbon import Res
from Carbon import Dlg
import sys
import socket
import string
import macresource
#
# Definitions for our resources
ID_MAIN=512

ITEM_LOOKUP_ENTRY=1
ITEM_RESULT=2
ITEM_LOOKUP_BUTTON=3
ITEM_QUIT_BUTTON=4

def main():
    """Main routine: open resource file, call dialog handler"""
    macresource.need("DLOG", ID_MAIN, "dnslookup-1.rsrc")
    do_dialog()

def do_dialog():
    """Post dialog and handle user interaction until quit"""
    my_dlg = Dlg.GetNewDialog(ID_MAIN, -1)
    while 1:
        n = Dlg.ModalDialog(None)
        if n == ITEM_LOOKUP_BUTTON:
            tp, h, rect = my_dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
            txt = Dlg.GetDialogItemText(h)

            tp, h, rect = my_dlg.GetDialogItem(ITEM_RESULT)
            Dlg.SetDialogItemText(h, dnslookup(txt))
        elif n == ITEM_QUIT_BUTTON:
            break

def dnslookup(str):
    """ Perform DNS lookup on str.  If first character of digit is numeric,
        assume that str contains an IP address.  Otherwise, assume that str
        contains a hostname."""
    if str == '': str = ' '
    if str[0] in string.digits:
        try:
            value = socket.gethostbyaddr(str)[0]
        except:
            value = 'Lookup failed'
    else:
        try:
            value = socket.gethostbyname(str)
        except:
            value = 'Lookup failed'
    return value

main()