以两种不同的方法使用单个 telnet 会话

发布于 2024-11-04 06:59:09 字数 1599 浏览 1 评论 0原文

我需要使用像下面的代码一样的 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

问题是它似乎无法识别在我的第一个方法“connect”中打开的 telnet 会话...我们怎样才能以最好的方式做到这一点?

I need to use a telnet session like in this code below :

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()

Here ise the error raised :

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

The problem is that it seems to not recognize the telnet session open in my first method "connect"...How can we do this the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-11-11 06:59:09

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

tn should be self.tn so that the other methods can access it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文