diff --git a/setup.py b/setup.py index 1595125..2cca738 100644 --- a/setup.py +++ b/setup.py @@ -5,9 +5,9 @@ def readme(): return f.read() setup( - name = "telnetsrv", + name = "telnetsrv3", packages = ["telnetsrv"], - version = "0.4", + version = "0.5", extras_require = { 'green': ['gevent'], 'ssh': ['paramiko'], @@ -16,7 +16,7 @@ def readme(): long_description = readme(), author = "Ian Epperson", author_email = "ian@epperson.com", - url = "https://github.com/ianepperson/telnetsrvlib", + url = "https://github.com/Blindfreddy/telnetsrvlib3", keywords = ["gevent", "telnet", "server"], classifiers = [ "Programming Language :: Python", diff --git a/telnetsrv/green.py b/telnetsrv/green.py index ae461c8..dfe15fa 100644 --- a/telnetsrv/green.py +++ b/telnetsrv/green.py @@ -3,7 +3,7 @@ import gevent, gevent.queue -from telnetsrvlib import TelnetHandlerBase, command +from .telnetsrvlib import TelnetHandlerBase, command class TelnetHandler(TelnetHandlerBase): "A telnet server handler using Gevent" diff --git a/telnetsrv/paramiko_ssh.py b/telnetsrv/paramiko_ssh.py index c5a8b72..a756863 100644 --- a/telnetsrv/paramiko_ssh.py +++ b/telnetsrv/paramiko_ssh.py @@ -1,7 +1,10 @@ import logging #from binascii import hexlify from threading import Thread -from SocketServer import BaseRequestHandler +if sys.version_info > (3, 0): + from socketserver import BaseRequestHandler +else: + from SocketServer import BaseRequestHandler from paramiko import Transport, ServerInterface, RSAKey, DSSKey, SSHException, \ AUTH_SUCCESSFUL, AUTH_FAILED, \ @@ -88,7 +91,7 @@ def setup(self): # Tell transport to use this object as a server log.debug( 'Starting SSH server-side negotiation' ) self.transport.start_server(server=self) - except SSHException, e: + except (SSHException) as e: log.warn('SSH negotiation failed. %s', e) raise diff --git a/telnetsrv/telnetsrvlib.py b/telnetsrv/telnetsrvlib.py index d45b714..573918b 100755 --- a/telnetsrv/telnetsrvlib.py +++ b/telnetsrv/telnetsrvlib.py @@ -22,7 +22,11 @@ Function.aliases may be a list of alternative spellings """ -import SocketServer +import sys +if sys.version_info > (3, 0): + from socketserver import BaseRequestHandler +else: + from SocketServer import BaseRequestHandler import socket import struct import sys @@ -367,7 +371,7 @@ def process(self, line): self.process( self.handler.readline(prompt=self.handler.CONTINUE_PROMPT) ) -class TelnetHandlerBase(SocketServer.BaseRequestHandler): +class TelnetHandlerBase(BaseRequestHandler): "A telnet server based on the client in telnetlib" # Several methods are not fully defined in this class, and are @@ -481,7 +485,7 @@ def __init__(self, request, client_address, server): for alias in getattr(method, "aliases", []): self.COMMANDS[alias.upper()] = self.COMMANDS[name] - SocketServer.BaseRequestHandler.__init__(self, request, client_address, server) + BaseRequestHandler.__init__(self, request, client_address, server) class false_request(object): def __init__(self): @@ -524,7 +528,10 @@ def setup(self): except: pass self.setterm(self.TERM) - self.sock = self.request._sock + if hasattr(self.request, '_sock'): + self.sock = self.request._sock + else: + self.sock = self.request for k in self.DOACK.keys(): self.sendcommand(self.DOACK[k], k) for k in self.WILLACK.keys(): @@ -552,14 +559,14 @@ def options_handler(self, sock, cmd, opt): if cmd == NOP: self.sendcommand(NOP) elif cmd == WILL or cmd == WONT: - if self.WILLACK.has_key(opt): + if opt in self.WILLACK: self.sendcommand(self.WILLACK[opt], opt) else: self.sendcommand(DONT, opt) if cmd == WILL and opt == TTYPE: self.writecooked(IAC + SB + TTYPE + SEND + IAC + SE) elif cmd == DO or cmd == DONT: - if self.DOACK.has_key(opt): + if opt in self.DOACK: self.sendcommand(self.DOACK[opt], opt) else: self.sendcommand(WONT, opt) @@ -582,14 +589,14 @@ def options_handler(self, sock, cmd, opt): def sendcommand(self, cmd, opt=None): "Send a telnet command (IAC)" if cmd in [DO, DONT]: - if not self.DOOPTS.has_key(opt): + if opt not in self.DOOPTS: self.DOOPTS[opt] = None if (((cmd == DO) and (self.DOOPTS[opt] != True)) or ((cmd == DONT) and (self.DOOPTS[opt] != False))): self.DOOPTS[opt] = (cmd == DO) self.writecooked(IAC + cmd + opt) elif cmd in [WILL, WONT]: - if not self.WILLOPTS.has_key(opt): + if not opt in self.WILLOPTS: self.WILLOPTS[opt] = '' if (((cmd == WILL) and (self.WILLOPTS[opt] != True)) or ((cmd == WONT) and (self.WILLOPTS[opt] != False))): @@ -803,7 +810,10 @@ def write(self, text): def writecooked(self, text): """Put data directly into the output queue (bypass output cooker)""" - self.sock.sendall(text) + if sys.version_info > (3, 0): + self.sock.sendall(text.encode('latin1')) + else: + self.sock.sendall(text) # ------------------------------- Input Cooker ----------------------------- def _inputcooker_getc(self, block=True): @@ -817,7 +827,10 @@ def _inputcooker_getc(self, block=True): if not block: if not self.inputcooker_socket_ready(): return '' - ret = self.sock.recv(20) + if sys.version_info > (3, 0): + ret = self.sock.recv(20).decode('latin1') + else: + ret = self.sock.recv(20) self.eof = not(ret) self.rawq = self.rawq + ret if self.eof: @@ -945,8 +958,7 @@ def cmdHELP(self, params): self.writeline("Command '%s' not known" % cmd) else: self.writeline("Help on built in commands\n") - keys = self.COMMANDS.keys() - keys.sort() + keys = sorted(self.COMMANDS.keys()) for cmd in keys: method = self.COMMANDS[cmd] if getattr(method, 'hidden', False): @@ -1037,7 +1049,7 @@ def handle(self): if self.input.cmd: cmd = self.input.cmd.upper() params = self.input.params - if self.COMMANDS.has_key(cmd): + if cmd in self.COMMANDS: try: self.COMMANDS[cmd](params) except: diff --git a/telnetsrv/threaded.py b/telnetsrv/threaded.py index 3f7a2e7..40f7141 100755 --- a/telnetsrv/threaded.py +++ b/telnetsrv/threaded.py @@ -5,7 +5,7 @@ import time import select -from telnetsrvlib import TelnetHandlerBase, command +from .telnetsrvlib import TelnetHandlerBase, command class TelnetHandler(TelnetHandlerBase): "A telnet server handler using Threading"