source: rtems-tools/tester/rt/telnet.py @ ac679ba

5
Last change on this file since ac679ba was ac679ba, checked in by Chris Johns <chrisj@…>, on 10/21/17 at 05:09:00

tester: Print the host and port on error in telnet connect.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2013-2017 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# RTEMS Testing Consoles
33#
34
35import errno
36import os
37import sys
38import telnetlib
39
40from rtemstoolkit import error
41from rtemstoolkit import host
42from rtemstoolkit import path
43
44class tty:
45
46    def __init__(self, dev):
47        self.dev = dev
48        self.timeout = 5
49        self.conn = None
50        ds = dev.split(':')
51        self.host = ds[0]
52        if len(ds) == 1:
53            self.port = 23
54        else:
55            try:
56                self.port = int(ds[1])
57            except:
58                raise error.general('invalid port: %s' % (dev))
59        self.conn = telnetlib.Telnet()
60        self._reopen()
61
62    def __del__(self):
63        if self.conn:
64            try:
65                self.conn.close()
66            except:
67                pass
68
69    def __str__(self):
70        s = 'host: %s port: %d' % ((self.host, self.port))
71        return s
72
73    def _reopen(self):
74        try:
75            self.conn.open(self.host, self.port, self.timeout)
76        except IOError as ioe:
77            raise error.general('opening telnet: %s:%d: %s' % (self.host,
78                                                               self.port,
79                                                               ioe))
80        except:
81            raise error.general('opening telnet: %s:%d: unknown' % (self.host,
82                                                                    self.port))
83
84    def off(self):
85        self.is_on = False
86
87    def on(self):
88        self.is_on = True
89
90    def set(self, flags):
91        pass
92
93    def read(self):
94        reopen = False
95        try:
96            data = self.conn.read_very_eager()
97        except IOError as ioe:
98            if ioe.errno == errno.ECONNREFUSED:
99                reopen = True
100            data = ''
101        except EOFError:
102            reopen = True
103            data = ''
104        if reopen:
105            self._reopen()
106        return data
107
108if __name__ == "__main__":
109    if len(sys.argv) == 2:
110        import time
111        t = tty(sys.argv[1])
112        print(t)
113        while True:
114            time.sleep(0.05)
115            c = t.read()
116            sys.stdout.write(c)
Note: See TracBrowser for help on using the repository browser.