py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Demo / example2 / dnslookup-2.py
blobe595b8e2da78b677ede3a247d48764d881092d24
1 import FrameWork
2 import EasyDialogs
3 import Res
4 import Dlg
5 import sys
6 import socket
7 import string
9 # Definitions for our resources
10 ID_MAIN=512
11 ID_ABOUT=513
13 ITEM_LOOKUP_ENTRY=1
14 ITEM_RESULT=2
15 ITEM_LOOKUP_BUTTON=3
17 def main():
18 try:
19 dummy = Res.GetResource('DLOG', ID_MAIN)
20 except Res.Error:
21 try:
22 Res.FSpOpenResFile("dnslookup-2.rsrc", 1)
23 except Res.Error:
24 EasyDialogs.Message("Cannot open dnslookup-2.rsrc")
25 sys.exit(1)
26 DNSLookup()
28 class DNSLookup(FrameWork.Application):
29 "Application class for DNS Lookup"
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()
41 def makeusermenus(self):
42 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
43 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
45 def quit(self, *args):
46 self._quit()
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
55 class MyDialog(FrameWork.DialogWindow):
56 "Main dialog window for DNSLookup"
57 def __init__(self, parent):
58 FrameWork.DialogWindow.__init__(self, parent)
59 self.parent = parent
61 def do_itemhit(self, item, event):
62 if item == ITEM_LOOKUP_BUTTON:
63 self.dolookup()
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."""
68 tp, h, rect = self.dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
69 txt = Dlg.GetDialogItemText(h)
71 tp, h, rect = self.dlg.GetDialogItem(ITEM_RESULT)
72 Dlg.SetDialogItemText(h, self.dnslookup(txt))
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
92 main()