{{{ #!python from wxPython.wx import * from wxPython.lib.layoutf import Layoutf from telnetlib import Telnet # core module #--------------------------------------------------------------------------- class UpdateTimer(wxTimer): def __init__(self, target, dur=1000): wxTimer.__init__(self) self.target = target self.Start(dur) def Notify(self): """Called every timer interval""" if self.target: self.target.OnUpdate() #--------------------------------------------------------------------------- class TelnetFrame(wxFrame): def __init__(self, parent, ID, host): wxFrame.__init__(self, parent, ID, "Telnet - " + host) self.SetAutoLayout(true) font = wxFont(8, wxMODERN, wxNORMAL, wxNORMAL, faceName="Lucida Console") commandId = wxNewId() self.command = wxTextCtrl(self, commandId, "[command]", wxPyDefaultPosition, wxPyDefaultSize, wxTE_PROCESS_ENTER) self.command.SetConstraints(Layoutf('t=t2#1;r=r2#1;b!32;l=l2#1', (self,))) self.command.SetFont(font) EVT_TEXT_ENTER(self, commandId, self.OnEnter) # Select the text so that it's automatically typed over. self.command.SetSelection(0, self.command.GetLastPosition()) self.output = wxTextCtrl(self, -1, "", wxPyDefaultPosition, wxPyDefaultSize, wxTE_MULTILINE|wxTE_READONLY) self.output.SetConstraints(Layoutf('t_2#2;r=r2#1;b=b2#1;l=l2#1', (self,self.command))) self.output.SetFont(font) EVT_CLOSE(self, self.OnCloseWindow) self.tn = Telnet(host) # connect to host self.timer = UpdateTimer(self, 200) # update five times a second def OnUpdate(self): """Output what's in the telnet buffer""" try: newtext = self.tn.read_very_eager() if newtext: lines = newtext.split('\n') # This keeps it from printing out extra blank lines. for line in lines: self.output.AppendText(line) except EOFError: self.tn.close() self.timer.Stop() self.output.AppendText("Disconnected by remote host") def OnEnter(self, e): """The user has entered a command. Send it!""" self.tn.write(e.GetString() + "\r\n") self.command.SetSelection(0, self.command.GetLastPosition()) def OnCloseWindow(self, event): """We're closing, so clean up.""" self.timer.Stop() self.Destroy() #--------------------------------------------------------------------------- if __name__ == '__main__': import sys app = wxPySimpleApp() frame = TelnetFrame(None, -1, sys.argv[1]) frame.Show(true) app.MainLoop() }}}