blob: 9747c4a83d78d34594cabe2b52165524e67ed6d1 [file] [log] [blame]
Jack Jansen2450a251999-12-03 15:15:28 +00001import FrameWork
2import EasyDialogs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00003from Carbon import Res
4from Carbon import Dlg
Jack Jansen2450a251999-12-03 15:15:28 +00005import sys
6import socket
7import string
Jack Jansen3c06b9a2001-08-27 21:41:23 +00008import macresource
Jack Jansen2450a251999-12-03 15:15:28 +00009#
10# Definitions for our resources
11ID_MAIN=512
12ID_ABOUT=513
13
14ITEM_LOOKUP_ENTRY=1
15ITEM_RESULT=2
16ITEM_LOOKUP_BUTTON=3
17
18def main():
Jack Jansen3c06b9a2001-08-27 21:41:23 +000019 macresource.need("DLOG", ID_MAIN, "dnslookup-2.rsrc")
Jack Jansen2450a251999-12-03 15:15:28 +000020 DNSLookup()
21
22class DNSLookup(FrameWork.Application):
23 "Application class for DNS Lookup"
24
25 def __init__(self):
26 # First init menus, etc.
27 FrameWork.Application.__init__(self)
28 # Next create our dialog
29 self.main_dialog = MyDialog(self)
30 # Now open the dialog
31 self.main_dialog.open(ID_MAIN)
32 # Finally, go into the event loop
33 self.mainloop()
34
35 def makeusermenus(self):
36 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
37 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
38
39 def quit(self, *args):
40 self._quit()
41
42 def do_about(self, *args):
43 f = Dlg.GetNewDialog(ID_ABOUT, -1)
44 while 1:
45 n = Dlg.ModalDialog(None)
46 if n == 1:
47 return
48
49class MyDialog(FrameWork.DialogWindow):
50 "Main dialog window for DNSLookup"
51 def __init__(self, parent):
52 FrameWork.DialogWindow.__init__(self, parent)
53 self.parent = parent
54
55 def do_itemhit(self, item, event):
56 if item == ITEM_LOOKUP_BUTTON:
57 self.dolookup()
58
59 def dolookup(self):
60 """Get text entered in the lookup entry area. Place result of the
61 call to dnslookup in the result entry area."""
Jack Jansened24cd22001-02-14 17:07:04 +000062 tp, h, rect = self.dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
Jack Jansen2450a251999-12-03 15:15:28 +000063 txt = Dlg.GetDialogItemText(h)
64
Jack Jansened24cd22001-02-14 17:07:04 +000065 tp, h, rect = self.dlg.GetDialogItem(ITEM_RESULT)
Jack Jansen2450a251999-12-03 15:15:28 +000066 Dlg.SetDialogItemText(h, self.dnslookup(txt))
67
68 def dnslookup(self, str):
69 """ Perform DNS lookup on str. If first character of digit is numeric,
70 assume that str contains an IP address. Otherwise, assume that str
71 contains a hostname."""
72 if str == '': str = ' '
73 if str[0] in string.digits:
74 try:
75 value = socket.gethostbyaddr(str)[0]
76 except:
77 value = 'Lookup failed'
78 else:
79 try:
80 value = socket.gethostbyname(str)
81 except:
82 value = 'Lookup failed'
83 return value
84
85
86main()