From cd918d0dc662777f8f4ebc717496e5f4e65f3502 Mon Sep 17 00:00:00 2001 From: Scott Maxwell Date: Wed, 30 Oct 2013 16:07:03 -0700 Subject: [PATCH] Add Py3 helpers to common.py --- paramiko/common.py | 47 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/paramiko/common.py b/paramiko/common.py index 3d7ca58..013510e 100644 --- a/paramiko/common.py +++ b/paramiko/common.py @@ -19,12 +19,13 @@ """ Common constants and global variables. """ +from paramiko.py3compat import * MSG_DISCONNECT, MSG_IGNORE, MSG_UNIMPLEMENTED, MSG_DEBUG, MSG_SERVICE_REQUEST, \ MSG_SERVICE_ACCEPT = range(1, 7) MSG_KEXINIT, MSG_NEWKEYS = range(20, 22) MSG_USERAUTH_REQUEST, MSG_USERAUTH_FAILURE, MSG_USERAUTH_SUCCESS, \ - MSG_USERAUTH_BANNER = range(50, 54) + MSG_USERAUTH_BANNER = range(50, 54) MSG_USERAUTH_PK_OK = 60 MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE = range(60, 62) MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE = range(80, 83) @@ -33,6 +34,10 @@ MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \ MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MSG_CHANNEL_REQUEST, \ MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE = range(90, 101) +for key in list(globals().keys()): + if key.startswith('MSG_'): + globals()['c' + key] = byte_chr(globals()[key]) +del key # for debugging: MSG_NAMES = { @@ -69,7 +74,7 @@ MSG_NAMES = { MSG_CHANNEL_REQUEST: 'channel-request', MSG_CHANNEL_SUCCESS: 'channel-success', MSG_CHANNEL_FAILURE: 'channel-failure' - } +} # authentication request return codes: @@ -118,6 +123,44 @@ else: import logging PY22 = False +if PY3: + zero_byte = b'\x00' + one_byte = b'\x01' + four_byte = b'\x04' + max_byte = b'\xff' + newline_byte = b'\n' + cr_byte = b'\r' +else: + zero_byte = '\x00' + one_byte = '\x01' + four_byte = '\x04' + max_byte = '\xff' + newline_byte = '\n' + cr_byte = '\r' + + +def asbytes(s): + if not isinstance(s, bytes_type): + if isinstance(s, string_types): + s = b(s) + else: + try: + s = s.asbytes() + except Exception: + raise Exception('Unknown type') + return s + +xffffffff = long(0xffffffff) +x80000000 = long(0x80000000) +long_zero = long(0) +long_one = long(1) +o666 = 438 +o660 = 432 +o644 = 420 +o600 = 384 +o777 = 511 +o700 = 448 +o70 = 56 DEBUG = logging.DEBUG INFO = logging.INFO