Skip to content

Commit

Permalink
Merge pull request #10 from kjoconnor/naws
Browse files Browse the repository at this point in the history
Adding NAWS to initial connection, available at self.WIDTH and self.HEIGHT
  • Loading branch information
ianepperson committed Dec 18, 2013
2 parents 3b9c87f + 873172a commit 997f60b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ These will be provided for inspection.

``TERM``
String ID describing the currently connected terminal

``WIDTH``
Integer describing the width of the terminal at connection time.

``HEIGHT``
Integer describing the height of the terminal at connection time.

``username``
Set after authentication succeeds, name of the logged in user.
Expand All @@ -234,6 +240,7 @@ These will be provided for inspection.
Provides some information about the current terminal.
'''
self.writeresponse( "Username: %s, terminal type: %s" % (self.username, self.TERM) )
self.writeresponse( "Width: %s, height: %s" % (self.WIDTH, self.HEIGHT) )
self.writeresponse( "Command history:" )
for c in self.history:
self.writeresponse(" %r" % c)
Expand Down Expand Up @@ -375,7 +382,7 @@ others).
import gevent.server
server = gevent.server.StreamServer(("", 8023), MyHandler.streamserver_handle)
server.server_forever()
server.serve_forever()
Short Example
Expand Down
10 changes: 9 additions & 1 deletion telnetsrv/telnetsrvlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import SocketServer
import socket
import struct
import sys
import traceback
import curses.ascii
import curses.has_key
import curses
import logging
import re
#if not hasattr(socket, 'SHUT_RDWR'):
# socket.SHUT_RDWR = 2

Expand Down Expand Up @@ -489,6 +489,12 @@ def streamserver_handle(cls, socket, address):
log.debug("Accepted connection, starting telnet session.")
cls(request, address, server)

def setnaws(self, naws):
''' Set width and height of the terminal on initial connection'''
self.WIDTH = struct.unpack('>h', naws[0:2])[0]
self.HEIGHT = struct.unpack('>h', naws[2:4])[0]
log.debug("Set width to %s and height to %s" % (self.WIDTH, self.HEIGHT))

def setterm(self, term):
"Set the curses structures for this terminal"
log.debug("Setting termtype to %s" % (term, ))
Expand Down Expand Up @@ -560,6 +566,8 @@ def options_handler(self, sock, cmd, opt):
self.setterm(subreq[2:])
except:
log.debug("Terminal type not known")
elif subreq[0] == NAWS:
self.setnaws(subreq[1:])
elif cmd == SB:
pass
else:
Expand Down

0 comments on commit 997f60b

Please sign in to comment.