From: Jan Rękorajski Date: Sun, 17 Jan 2021 10:01:48 +0000 (+0100) Subject: Send and receive bytes over sockets (python3 compat) X-Git-Url: https://git.pld-linux.org/?p=projects%2Fpld-ftp-admin.git;a=commitdiff_plain;h=8d9e10143107c972aa7f35f1d2cc51cc2b111684 Send and receive bytes over sockets (python3 compat) --- diff --git a/modules/cmds.py b/modules/cmds.py index d82b35a..e7dc879 100644 --- a/modules/cmds.py +++ b/modules/cmds.py @@ -38,18 +38,18 @@ def parse(con): def lock(con, arg, hard): if arg not in locks: locks[arg]={'hard': hard, 'name': con.name, 'time': int(time.time())} - con.sock.send("OK") + con.sock.send(bytearray("OK", encoding='utf-8')) elif locks[arg]['hard']: - con.sock.send("HARD") # Hard lock - you can go get a cup of tea + con.sock.send(bytearray("HARD", encoding='utf-8')) # Hard lock - you can go get a cup of tea else: - con.sock.send("SOFT") # Soft lock - try in a second or two + con.sock.send(bytearray("SOFT", encoding='utf-8')) # Soft lock - try in a second or two def cmd_unlock(con, arg): if arg in locks: del locks[arg] - con.sock.send("OK") + con.sock.send(bytearray("OK", encoding='utf-8')) else: - con.sock.send("FAIL") + con.sock.send(bytearray("FAIL", encoding='utf-8')) def cmd_lock_soft(con, arg): lock(con, arg, False) @@ -67,11 +67,11 @@ def cmd_show_locks(con): tree, data['name'], data['hard'], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data['time']))) cmd_log(con, msg) res = res + msg -# con.sock.send("BLOB:%d" % len(res)) - con.sock.send(res) +# con.sock.send(bytearray("BLOB:%d" % len(res), encoding='utf-8'))) + con.sock.send(bytearray(res, encoding='utf-8')) else: - cmd_log(con, "No locks found."); - con.sock.send("NLCK"); + cmd_log(con, "No locks found.") + con.sock.send(bytearray("NLCK", encoding='utf-8')) def cmd_log(con, msg): logfile.write('%s [%s] -- %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), con.name, msg)) @@ -122,10 +122,10 @@ def cmd_login_passwd(con, data): write_cookies() con.username=login con.authorized=True - con.sock.send('OK '+cookie) + con.sock.send(bytearray('OK '+cookie, encoding='utf-8')) else: # TODO: log this - con.sock.send('FAIL') + con.sock.send(bytearray('FAIL', encoding='utf-8')) raise BailOut() def cmd_login_cookie(con, cookie): @@ -133,10 +133,10 @@ def cmd_login_cookie(con, cookie): con.cookie=cookie con.authorized=True con.username=cookies[cookie] - con.sock.send('OK '+cookies[cookie]) + con.sock.send(bytearray('OK '+cookies[cookie], encoding='utf-8')) else: # TODO: log this (or not) - con.sock.send('FAIL') + con.sock.send(bytearray('FAIL')) def cmd_logout(con): if con.cookie in cookies: @@ -165,10 +165,10 @@ def cmd_gettree(con): line=line+'\n0' buf=buf+'\0'+line if buf: - con.sock.send('%.6d' % (len(buf)-1)) - con.sock.send(buf[1:]) + con.sock.send(bytearray('%.6d' % (len(buf)-1), encoding='utf-8')) + con.sock.send(bytearray(buf[1:], encoding='utf-8')) else: - con.sock.send('000000') + con.sock.send(bytearray('000000', encoding='utf-8')) cmdlist_args={'lcks':cmd_lock_soft, 'lckh':cmd_lock_hard, 'ulck':cmd_unlock, diff --git a/modules/cons.py b/modules/cons.py index 8213c68..dae0587 100644 --- a/modules/cons.py +++ b/modules/cons.py @@ -30,7 +30,7 @@ class Connection: if not newdata: self.destroy() else: - self.data = self.data + newdata + self.data = self.data + newdata.decode("utf-8") try: cmds.parse(self) diff --git a/modules/ftpio.py b/modules/ftpio.py index 2529cee..59cb55c 100644 --- a/modules/ftpio.py +++ b/modules/ftpio.py @@ -20,12 +20,12 @@ def connect(name=None): sock.connect(socketname) if not name: name = "pid_%d_name_%s" % (os.getpid(), sys.argv[0]) - sock.send('name %s\0' % name) + sock.send(bytearray('name %s\0' % name, encoding='utf-8')) def login_passwd(login, passwd): 'Return cookie if ok' - sock.send('linp %s\n%s\0' % (login, passwd)) - retval=sock.recv(256) + sock.send(bytearray('linp %s\n%s\0' % (login, passwd), encoding='utf-8')) + retval=sock.recv(256).decode("utf-8") if retval=='FAIL': return '' else: @@ -33,23 +33,23 @@ def login_passwd(login, passwd): def login_cookie(cookie): 'Return login if ok' - sock.send('linc %s\0' % cookie) - retval=sock.recv(256) + sock.send(bytearray('linc %s\0' % cookie, encoding='utf-8')) + retval=sock.recv(256).decode('utf-8') if retval=='FAIL': return '' else: return retval[3:] def logout(): - sock.send('lout\0') + sock.send(bytearray('lout\0', encoding='utf-8')) def lock(path, hard=False): def dolock(): if hard: - sock.send('lckh %s\0' % path) + sock.send(bytearray('lckh %s\0' % path, encoding='utf-8')) else: - sock.send('lcks %s\0' % path) - return sock.recv(20) + sock.send(bytearray('lcks %s\0' % path, encoding='utf-8')) + return sock.recv(20).decode("utf-8") for i in range(3): retcode=dolock() if retcode=="OK": @@ -61,31 +61,31 @@ def lock(path, hard=False): return False def unlock(path): - sock.send('ulck %s\0' % path) - ret = sock.recv(20) + sock.send(bytearray('ulck %s\0' % path, encoding='utf-8')) + ret = sock.recv(20).decode("utf-8") if ret == "OK": return True return False def log(msg): - sock.send('log1 %s\0' % msg) + sock.send(bytearray('log1 %s\0' % msg, encoding='utf-8')) def locks_dump(): - sock.send('slck\0') - ret = sock.recv(4096) + sock.send(bytearray('slck\0', encoding='utf-8')) + ret = sock.recv(4096).decode("utf-8") if ret == "NLCK": return "No locks held" # nbytes = int(ret.split("BLOB:")[1]) -# ret = sock.recv(nbytes) +# ret = sock.recv(nbytes).decode("utf-8") return ret def gettree(): - sock.send('gett\0') + sock.send(bytearray('gett\0', encoding='utf-8')) pkgs=[] - len=int(sock.recv(6)) + len=int(sock.recv(6).decode("utf-8")) if len: - for pkg in sock.recv(len).split('\0'): + for pkg in sock.recv(len).decode("utf-8").split('\0'): tmp=pkg.split('\n') pkgs.append((tmp[0], int(tmp[1]), int(tmp[2]))) return pkgs