blob: e595b8e2da78b677ede3a247d48764d881092d24 [file] [log] [blame]
Jack Jansen2450a251999-12-03 15:15:28 +00001import FrameWork
2import EasyDialogs
3import Res
4import Dlg
5import sys
6import socket
7import string
8#
9# Definitions for our resources
10ID_MAIN=512
11ID_ABOUT=513
12
13ITEM_LOOKUP_ENTRY=1
14ITEM_RESULT=2
15ITEM_LOOKUP_BUTTON=3
16
17def main():
18 try:
19 dummy = Res.GetResource('DLOG', ID_MAIN)
20 except Res.Error:
21 try:
Jack Jansen8eff33b2000-06-20 22:01:04 +000022 Res.FSpOpenResFile("dnslookup-2.rsrc", 1)
Jack Jansen2450a251999-12-03 15:15:28 +000023 except Res.Error:
24 EasyDialogs.Message("Cannot open dnslookup-2.rsrc")
25 sys.exit(1)
26 DNSLookup()
27
28class DNSLookup(FrameWork.Application):
29 "Application class for DNS Lookup"
30
31 def __init__(self):
32 # First init menus, etc.
33 FrameWork.Application.__init__(self)
34 # Next create our dialog
35 self.main_dialog = MyDialog(self)
36 # Now open the dialog
37 self.main_dialog.open(ID_MAIN)
38 # Finally, go into the event loop
39 self.mainloop()
40
41 def makeusermenus(self):
42 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
43 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
44
45 def quit(self, *args):
46 self._quit()
47
48 def do_about(self, *args):
49 f = Dlg.GetNewDialog(ID_ABOUT, -1)
50 while 1:
51 n = Dlg.ModalDialog(None)
52 if n == 1:
53 return
54
55class MyDialog(FrameWork.DialogWindow):
56 "Main dialog window for DNSLookup"
57 def __init__(self, parent):
58 FrameWork.DialogWindow.__init__(self, parent)
59 self.parent = parent
60
61 def do_itemhit(self, item, event):
62 if item == ITEM_LOOKUP_BUTTON:
63 self.dolookup()
64
65 def dolookup(self):
66 """Get text entered in the lookup entry area. Place result of the
67 call to dnslookup in the result entry area."""
Jack Jansened24cd22001-02-14 17:07:04 +000068 tp, h, rect = self.dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
Jack Jansen2450a251999-12-03 15:15:28 +000069 txt = Dlg.GetDialogItemText(h)
70
Jack Jansened24cd22001-02-14 17:07:04 +000071 tp, h, rect = self.dlg.GetDialogItem(ITEM_RESULT)
Jack Jansen2450a251999-12-03 15:15:28 +000072 Dlg.SetDialogItemText(h, self.dnslookup(txt))
73
74 def dnslookup(self, str):
75 """ Perform DNS lookup on str. If first character of digit is numeric,
76 assume that str contains an IP address. Otherwise, assume that str
77 contains a hostname."""
78 if str == '': str = ' '
79 if str[0] in string.digits:
80 try:
81 value = socket.gethostbyaddr(str)[0]
82 except:
83 value = 'Lookup failed'
84 else:
85 try:
86 value = socket.gethostbyname(str)
87 except:
88 value = 'Lookup failed'
89 return value
90
91
92main()