summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xexamples/rfc2217_server.py93
1 files changed, 45 insertions, 48 deletions
diff --git a/examples/rfc2217_server.py b/examples/rfc2217_server.py
index fb89627..a8685f4 100755
--- a/examples/rfc2217_server.py
+++ b/examples/rfc2217_server.py
@@ -3,7 +3,7 @@
# redirect data from a TCP/IP connection to a serial port and vice versa
# using RFC 2217
#
-# (C) 2009 Chris Liechti <cliechti@gmx.net>
+# (C) 2009-2015 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier: BSD-3-Clause
@@ -17,15 +17,15 @@ import serial
import serial.rfc2217
class Redirector(object):
- def __init__(self, serial_instance, socket, debug=None):
+ def __init__(self, serial_instance, socket, debug=False):
self.serial = serial_instance
self.socket = socket
self._write_lock = threading.Lock()
self.rfc2217 = serial.rfc2217.PortManager(
- self.serial,
- self,
- logger = (debug and logging.getLogger('rfc2217.server'))
- )
+ self.serial,
+ self,
+ logger = logging.getLogger('rfc2217.server') if debug else None
+ )
self.log = logging.getLogger('redirector')
def statusline_poller(self):
@@ -35,7 +35,7 @@ class Redirector(object):
self.rfc2217.check_modem_lines()
self.log.debug('status line poll thread terminated')
- def shortcut(self):
+ def shortcircuit(self):
"""connect the serial port to the TCP port by copying everything
from one side to the other"""
self.alive = True
@@ -99,10 +99,9 @@ class Redirector(object):
if __name__ == '__main__':
- import optparse
+ import argparse
- parser = optparse.OptionParser(
- usage = "%prog [options] port",
+ parser = argparse.ArgumentParser(
description = "RFC 2217 Serial to Network (TCP/IP) redirector.",
epilog = """\
NOTE: no security measures are implemented. Anyone can remotely connect
@@ -112,52 +111,50 @@ Only one connection at once is supported. When the connection is terminated
it waits for the next connect.
""")
- parser.add_option("-p", "--localport",
- dest = "local_port",
- action = "store",
- type = 'int',
- help = "local TCP port",
- default = 2217
- )
+ parser.add_argument('SERIALPORT')
- parser.add_option("-v", "--verbose",
- dest = "verbosity",
- action = "count",
- help = "print more diagnostic messages (option can be given multiple times)",
- default = 0
- )
+ parser.add_argument('-p', '--localport',
+ type=int,
+ help='local TCP port, default: %(default)s',
+ metavar='TCPPORT',
+ default=2217
+ )
- (options, args) = parser.parse_args()
+ parser.add_argument('-v', '--verbose',
+ dest='verbosity',
+ action='count',
+ help='print more diagnostic messages (option can be given multiple times)',
+ default=0
+ )
- if len(args) != 1:
- parser.error('serial port name required as argument')
+ args = parser.parse_args()
- if options.verbosity > 3:
- options.verbosity = 3
+ if args.verbosity > 3:
+ args.verbosity = 3
level = (
- logging.WARNING,
- logging.INFO,
- logging.DEBUG,
- logging.NOTSET,
- )[options.verbosity]
+ logging.WARNING,
+ logging.INFO,
+ logging.DEBUG,
+ logging.NOTSET,
+ )[args.verbosity]
logging.basicConfig(level=logging.INFO)
logging.getLogger('root').setLevel(logging.INFO)
logging.getLogger('rfc2217').setLevel(level)
# connect to serial port
ser = serial.Serial()
- ser.port = args[0]
+ ser.port = args.SERIALPORT
ser.timeout = 3 # required so that the reader thread can exit
logging.info("RFC 2217 TCP/IP to Serial redirector - type Ctrl-C / BREAK to quit")
try:
ser.open()
- except serial.SerialException, e:
- logging.error("Could not open serial port %s: %s" % (ser.portstr, e))
+ except serial.SerialException as e:
+ logging.error("Could not open serial port %s: %s" % (ser.name, e))
sys.exit(1)
- logging.info("Serving serial port: %s" % (ser.portstr,))
+ logging.info("Serving serial port: %s" % (ser.name,))
settings = ser.getSettingsDict()
# reset control line as no _remote_ "terminal" has been connected yet
ser.setDTR(False)
@@ -165,33 +162,33 @@ it waits for the next connect.
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- srv.bind( ('', options.local_port) )
+ srv.bind(('', args.localport))
srv.listen(1)
- logging.info("TCP/IP port: %s" % (options.local_port,))
+ logging.info("TCP/IP port: %s" % (args.localport,))
while True:
try:
connection, addr = srv.accept()
logging.info('Connected by %s:%s' % (addr[0], addr[1]))
- connection.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
+ connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
ser.setRTS(True)
ser.setDTR(True)
# enter network <-> serial loop
r = Redirector(
- ser,
- connection,
- options.verbosity > 0
- )
+ ser,
+ connection,
+ args.verbosity > 0
+ )
try:
- r.shortcut()
+ r.shortcircuit()
finally:
logging.info('Disconnected')
r.stop()
connection.close()
ser.setDTR(False)
ser.setRTS(False)
- # Restore port settings (may have been changed by RFC 2217 capable
- # client)
- ser.applySettingsDict(settings)
+ # Restore port settings (may have been changed by RFC 2217
+ # capable client)
+ ser.applySettingsDict(settings)
except KeyboardInterrupt:
break
except socket.error as msg: