1999-12-03 11:15:28 -04:00
|
|
|
"""Sample program performing domain name lookups and showing off EasyDialogs,
|
|
|
|
Res and Dlg in the process"""
|
|
|
|
|
|
|
|
import EasyDialogs
|
2001-08-25 09:15:04 -03:00
|
|
|
from Carbon import Res
|
|
|
|
from Carbon import Dlg
|
1999-12-03 11:15:28 -04:00
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import string
|
2001-08-27 18:41:23 -03:00
|
|
|
import macresource
|
1999-12-03 11:15:28 -04:00
|
|
|
#
|
|
|
|
# Definitions for our resources
|
|
|
|
ID_MAIN=512
|
|
|
|
|
|
|
|
ITEM_LOOKUP_ENTRY=1
|
|
|
|
ITEM_RESULT=2
|
|
|
|
ITEM_LOOKUP_BUTTON=3
|
|
|
|
ITEM_QUIT_BUTTON=4
|
2001-08-27 18:41:23 -03:00
|
|
|
|
1999-12-03 11:15:28 -04:00
|
|
|
def main():
|
|
|
|
"""Main routine: open resource file, call dialog handler"""
|
2001-08-27 18:41:23 -03:00
|
|
|
macresource.need("DLOG", ID_MAIN, "dnslookup-1.rsrc")
|
1999-12-03 11:15:28 -04:00
|
|
|
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()
|
|
|
|
|