Avoid signed overflow in some xrange calculations, and extend
[python.git] / Mac / Demo / example2 / dnslookup-2.py
blobfc3db668cce97c7b70969fdc5a5fab1aef45e124
1 import FrameWork
2 import EasyDialogs
3 from Carbon import Res
4 from Carbon import Dlg
5 import socket
6 import string
7 import macresource
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 macresource.need("DLOG", ID_MAIN, "dnslookup-2.rsrc")
19 DNSLookup()
21 class DNSLookup(FrameWork.Application):
22 "Application class for DNS Lookup"
24 def __init__(self):
25 # First init menus, etc.
26 FrameWork.Application.__init__(self)
27 # Next create our dialog
28 self.main_dialog = MyDialog(self)
29 # Now open the dialog
30 self.main_dialog.open(ID_MAIN)
31 # Finally, go into the event loop
32 self.mainloop()
34 def makeusermenus(self):
35 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
36 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
38 def quit(self, *args):
39 self._quit()
41 def do_about(self, *args):
42 f = Dlg.GetNewDialog(ID_ABOUT, -1)
43 while 1:
44 n = Dlg.ModalDialog(None)
45 if n == 1:
46 return
48 class MyDialog(FrameWork.DialogWindow):
49 "Main dialog window for DNSLookup"
50 def __init__(self, parent):
51 FrameWork.DialogWindow.__init__(self, parent)
52 self.parent = parent
54 def do_itemhit(self, item, event):
55 if item == ITEM_LOOKUP_BUTTON:
56 self.dolookup()
58 def dolookup(self):
59 """Get text entered in the lookup entry area. Place result of the
60 call to dnslookup in the result entry area."""
61 tp, h, rect = self.dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
62 txt = Dlg.GetDialogItemText(h)
64 tp, h, rect = self.dlg.GetDialogItem(ITEM_RESULT)
65 Dlg.SetDialogItemText(h, self.dnslookup(txt))
67 def dnslookup(self, str):
68 """ Perform DNS lookup on str. If first character of digit is numeric,
69 assume that str contains an IP address. Otherwise, assume that str
70 contains a hostname."""
71 if str == '': str = ' '
72 if str[0] in string.digits:
73 try:
74 value = socket.gethostbyaddr(str)[0]
75 except:
76 value = 'Lookup failed'
77 else:
78 try:
79 value = socket.gethostbyname(str)
80 except:
81 value = 'Lookup failed'
82 return value
85 main()