source: trunk/essentials/dev-lang/python/Mac/Demo/example2/dnslookup-2.py@ 3404

Last change on this file since 3404 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 2.3 KB
Line 
1import FrameWork
2import EasyDialogs
3from Carbon import Res
4from Carbon import Dlg
5import sys
6import socket
7import string
8import macresource
9#
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():
19 macresource.need("DLOG", ID_MAIN, "dnslookup-2.rsrc")
20 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."""
62 tp, h, rect = self.dlg.GetDialogItem(ITEM_LOOKUP_ENTRY)
63 txt = Dlg.GetDialogItemText(h)
64
65 tp, h, rect = self.dlg.GetDialogItem(ITEM_RESULT)
66 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()
Note: See TracBrowser for help on using the repository browser.