diff --git a/serio.py b/serio.py index 16fae6d..68fc1fe 100755 --- a/serio.py +++ b/serio.py @@ -33,7 +33,7 @@ def put(self, source, destination): dpart = '' while j < self.BYTES_PER_LINE and i < data_size: - dpart += '\\x%.2X' % int(ord(data[i])) + dpart += '\\x%.2X' % (data[i]) j+=1 i+=1 @@ -41,12 +41,12 @@ def put(self, source, destination): # Show upload status if not self.quiet: - print "%d / %d" % (i, data_size) + print("%d / %d" % (i, data_size)) return i def write(self, data): - self.s.write(data) + self.s.write(data.encode()) if data.endswith('\n'): # Have to give the target system time for disk/flash I/O time.sleep(self.IO_TIME) @@ -64,29 +64,31 @@ def __init__(self, host, port, login=None, passwd=None, time=None, quiet=None): self.s = telnetlib.Telnet(host, port, timeout=10) # We're not interested in matching input, just interested # in consuming it, until it stops - DONT_MATCH = "\xff\xff\xff" + DONT_MATCH = b"\xff\xff\xff" if login: print(self.s.read_until(DONT_MATCH, 0.5)) - self.s.write(login + "\n") + login += "\n" + self.s.write(login.encode()) print(self.s.read_until(DONT_MATCH, 0.5)) - self.s.write(passwd + "\n") + passwd += "\n" + self.s.write(passwd.encode()) # Skip shell banner print(self.s.read_until(DONT_MATCH, self.IO_TIME)) def usage(): - print '\nUsage: %s [OPTIONS]\n' % sys.argv[0] - print '\t-s, --source= Path to local file' - print '\t-d, --destination= Path to remote file' - print '\t --telnet= Upload via telnet instead of serial' - print '\t-p, --port= Serial port to use [/dev/ttyUSB0] or telnet port [23]' - print '\t-b, --baudrate= Serial port baud rate [115200]' - print '\t-t, --time= Time to wait between echo commands [0.1]' - print '\t --login= Login name for telnet' - print '\t --pass= Password for telnet' - print '\t-q, --quiet Supress status messages' - print '\t-h, --help Show help' - print '' + print('\nUsage: %s [OPTIONS]\n' % sys.argv[0]) + print('\t-s, --source= Path to local file') + print('\t-d, --destination= Path to remote file') + print('\t --telnet= Upload via telnet instead of serial') + print('\t-p, --port= Serial port to use [/dev/ttyUSB0] or telnet port [23]') + print('\t-b, --baudrate= Serial port baud rate [115200]') + print('\t-t, --time= Time to wait between echo commands [0.1]') + print('\t --login= Login name for telnet') + print('\t --pass= Password for telnet') + print('\t-q, --quiet Supress status messages') + print('\t-h, --help Show help') + print('') sys.exit(1) def main(): @@ -104,8 +106,8 @@ def main(): try: opts, args = GetOpt(sys.argv[1:],'p:b:s:d:t:qh', ['port=', 'baudrate=', 'source=', 'destination=', 'time=', 'quiet', 'help', 'telnet=', 'login=', 'pass=']) - except GetoptError, e: - print 'Usage error:', e + except GetoptError as e: + print('Usage error:', e) usage() for opt, arg in opts: @@ -131,7 +133,7 @@ def main(): usage() if not source or not destination: - print 'Usage error: must specify -s and -d options' + print('Usage error: must specify -s and -d options') usage() try: @@ -151,10 +153,9 @@ def main(): size = sftp.put(source, destination) sftp.close() - print 'Uploaded %d bytes from %s to %s' % (size, source, destination) - except Exception, e: - print "ERROR:", e - + print('Uploaded %d bytes from %s to %s' % (size, source, destination)) + except Exception as e: + print("ERROR:", e) if __name__ == '__main__': main()