2011-04-30 91 views
0

使用單一的telnet會話,我需要在下面這段代碼使用telnet會話,如:在兩種不同的方法

class ModTelnet(MXComm): 
def __init__(self): 
    MXComm.__init__(self) 

def _connect(self): 
    #connect to telnet session @ localhost port 4444 
    try: 
     HOST = "localhost" 
     port = "4444" 
    # tn = telnetlib.Telnet(HOST, port) 
     tn.open(HOST, port) 
    except: 
     print "Connection refused" 


def _receive(self): 
    #receive data (= msg) from telnet stdout 
    try: 
     data = tn.read_all() 
     return data 
    except tn.eof.ERR as ex: 
     if 'timeout error' not in ex.args[0]: 
      print 'Connection error:', ex 
      raise Disconnected() 

def _send(self, data): 
    #send command to telnet session 
    try:   
     tn.write(data + "\n") 
    except tn.socket.error as ex: 
     if 'timeout error' not in ex.args[0]: 
      print 'Connection error:', ex 
      raise Disconnected() 

這裏ISE錯誤提出:

QApplication: Invalid Display* argument 
Connection refused 
Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner 
    self.run() 
    File "/home/mk3/MODiMX27/PELM_Gui/src/modimx.py", line 188, in run 
    self._conn.try_get_data() 
    File "/home/mk3/MODiMX27/PELM_Gui/src/modimx.py", line 58, in try_get_data 
    rx_item = self._receive() 
    File "/home/mk3/MODiMX27/PELM_Gui/src/modimx.py", line 112, in _receive 
    except tn.eof.ERR as ex: 

NameError: global name 'tn' is not defined 

的問題是,它似乎無法識別在我的第一個方法「連接」中打開的telnet會話......我們如何以最佳方式做到這一點?

+0

爲什麼TN的實例註釋掉? – rzetterberg 2011-04-30 19:10:19

回答

2

tn應該self.tn以便其他方法可以訪問它

+0

感謝您的幫助! (我仍然學習很多:D) – user732663 2011-04-30 21:21:37

+0

感謝修復它! – user732663 2011-05-02 05:20:35