Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -16,7 +16,7 @@ def readme():
long_description = readme(),
author = "Ian Epperson",
author_email = "[email protected]",
url = "https://github.com/ianepperson/telnetsrvlib",
url = "https://github.com/Blindfreddy/telnetsrvlib3",
keywords = ["gevent", "telnet", "server"],
classifiers = [
"Programming Language :: Python",
Expand Down
2 changes: 1 addition & 1 deletion telnetsrv/green.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions telnetsrv/paramiko_ssh.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand Down Expand Up @@ -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

Expand Down
38 changes: 25 additions & 13 deletions telnetsrv/telnetsrvlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand All @@ -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))):
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion telnetsrv/threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down