1 from wxPython.wx import *
   2 from wxPython.lib.layoutf import Layoutf
   3 from telnetlib import Telnet # core module
   4 
   5 #---------------------------------------------------------------------------
   6 
   7 class UpdateTimer(wxTimer):
   8     def __init__(self, target, dur=1000):
   9         wxTimer.__init__(self)
  10         self.target = target
  11         self.Start(dur)
  12 
  13     def Notify(self):
  14         """Called every timer interval"""
  15         if self.target:
  16             self.target.OnUpdate()
  17 
  18 #---------------------------------------------------------------------------
  19 class TelnetFrame(wxFrame):
  20     def __init__(self, parent, ID, host):
  21         wxFrame.__init__(self, parent, ID, "Telnet - " + host)
  22         self.SetAutoLayout(true)
  23         font = wxFont(8, wxMODERN, wxNORMAL, wxNORMAL, faceName="Lucida Console")
  24 
  25         commandId = wxNewId()
  26         self.command = wxTextCtrl(self, commandId, "[command]",
  27                                   wxPyDefaultPosition, wxPyDefaultSize,
  28                                   wxTE_PROCESS_ENTER)
  29         self.command.SetConstraints(Layoutf('t=t2#1;r=r2#1;b!32;l=l2#1', (self,)))
  30         self.command.SetFont(font)
  31         EVT_TEXT_ENTER(self, commandId, self.OnEnter)
  32 
  33         # Select the text so that it's automatically typed over.
  34         self.command.SetSelection(0, self.command.GetLastPosition()) 
  35 
  36 
  37         self.output = wxTextCtrl(self, -1, "",
  38                                  wxPyDefaultPosition, wxPyDefaultSize,
  39                                  wxTE_MULTILINE|wxTE_READONLY)
  40         self.output.SetConstraints(Layoutf('t_2#2;r=r2#1;b=b2#1;l=l2#1', (self,self.command)))
  41         self.output.SetFont(font)
  42 
  43         EVT_CLOSE(self, self.OnCloseWindow)
  44 
  45         self.tn = Telnet(host) # connect to host
  46         self.timer = UpdateTimer(self, 200) # update five times a second
  47 
  48     def OnUpdate(self):
  49         """Output what's in the telnet buffer"""
  50         try:
  51             newtext = self.tn.read_very_eager()
  52             if newtext:
  53                 lines = newtext.split('\n') # This keeps it from printing out extra blank lines.
  54                 for line in lines:
  55                     self.output.AppendText(line)
  56         except EOFError:
  57             self.tn.close()
  58             self.timer.Stop()
  59             self.output.AppendText("Disconnected by remote host")
  60         
  61     def OnEnter(self, e):
  62         """The user has entered a command.  Send it!"""
  63         self.tn.write(e.GetString() + "\r\n")
  64         self.command.SetSelection(0, self.command.GetLastPosition()) 
  65         
  66     def OnCloseWindow(self, event):
  67         """We're closing, so clean up."""
  68         self.timer.Stop()
  69         self.Destroy()
  70 
  71 #---------------------------------------------------------------------------
  72 
  73 if __name__ == '__main__':
  74     import sys
  75     app = wxPySimpleApp()
  76     frame = TelnetFrame(None, -1, sys.argv[1])
  77     frame.Show(true)
  78     app.MainLoop()

WxHowtoSmallTelnetClient (last edited 2009-11-25 06:53:08 by vpn-8061f451)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.