Merge pull request #513 from drjbarker/python2-compat

Fix python2 server compatibility
This commit is contained in:
Brian May 2020-08-30 09:48:29 +10:00 committed by GitHub
commit 19f653df36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -7,8 +7,11 @@ z = zlib.decompressobj()
while 1:
name = sys.stdin.readline().strip()
if name:
name = name.decode("ASCII")
# python2 compat: in python2 sys.stdin.readline().strip() -> str
# in python3 sys.stdin.readline().strip() -> bytes
# (see #481)
if sys.version_info >= (3, 0):
name = name.decode("ASCII")
nbytes = int(sys.stdin.readline())
if verbosity >= 2:
sys.stderr.write('server: assembling %r (%d bytes)\n'

View File

@ -299,8 +299,8 @@ class FirewallClient:
raise Fatal('%r expected STARTED, got %r' % (self.argv, line))
def sethostip(self, hostname, ip):
assert(not re.search(rb'[^-\w\.]', hostname))
assert(not re.search(rb'[^0-9.]', ip))
assert(not re.search(br'[^-\w\.]', hostname))
assert(not re.search(br'[^0-9.]', ip))
self.pfile.write(b'HOST %s,%s\n' % (hostname, ip))
self.pfile.flush()

View File

@ -6,7 +6,25 @@ import time
import sys
import os
import platform
from shutil import which
if sys.version_info >= (3, 0):
from shutil import which
else:
# python2 compat: shutil.which is not available so we provide our
# own which command
def which(file, mode=os.F_OK | os.X_OK, path=None):
if path is not None:
search_paths = [path]
elif "PATH" in os.environ:
search_paths = os.environ["PATH"].split(os.pathsep)
else:
search_paths = os.defpath.split(os.pathsep)
for p in search_paths:
filepath = os.path.join(p, file)
if os.path.exists(filepath) and os.access(filepath, mode):
return filepath
return None
import sshuttle.ssnet as ssnet
import sshuttle.helpers as helpers