| 1 | import FrameWork
|
|---|
| 2 | import EasyDialogs
|
|---|
| 3 | from Carbon import Res
|
|---|
| 4 | from Carbon import Dlg
|
|---|
| 5 | import sys
|
|---|
| 6 | import socket
|
|---|
| 7 | import string
|
|---|
| 8 | import macresource
|
|---|
| 9 | #
|
|---|
| 10 | # Definitions for our resources
|
|---|
| 11 | ID_MAIN=512
|
|---|
| 12 | ID_ABOUT=513
|
|---|
| 13 |
|
|---|
| 14 | ITEM_LOOKUP_ENTRY=1
|
|---|
| 15 | ITEM_RESULT=2
|
|---|
| 16 | ITEM_LOOKUP_BUTTON=3
|
|---|
| 17 |
|
|---|
| 18 | def main():
|
|---|
| 19 | macresource.need("DLOG", ID_MAIN, "dnslookup-2.rsrc")
|
|---|
| 20 | DNSLookup()
|
|---|
| 21 |
|
|---|
| 22 | class 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 |
|
|---|
| 49 | class 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 |
|
|---|
| 86 | main()
|
|---|