【问题标题】:Using a single telnet session in two different methods以两种不同的方法使用单个 telnet 会话
【发布时间】:2011-08-16 04:26:38
【问题描述】:

我需要使用如下代码中的 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()

这是引发的错误:

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 会话...我们怎样才能以最好的方式做到这一点?

【问题讨论】:

  • 为什么tn的实例化被注释掉了?

标签: python telnet telnetlib


【解决方案1】:

tn 应该是self.tn 以便其他方法可以访问它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2014-05-19
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多