Skip to content

Commit fd92107

Browse files
committed
0.1.2 fixed unable to login on high-latency connections
1 parent 037ff99 commit fd92107

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ python:
33
- '3.6'
44
- '3.7'
55
- '3.8'
6+
- '3.9'
67

78
script: pytest
89

ndms2_client/connection.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ def run_command(self, command, *, group_change_expected=False) -> List[str]:
6767
def connect(self):
6868
"""Connect to the Telnet server."""
6969
try:
70-
self._telnet = Telnet(self._host, self._port, self._timeout)
71-
self._telnet.set_option_negotiation_callback(TelnetConnection.__set_max_window_size)
70+
self._telnet = Telnet()
71+
self._telnet.set_option_negotiation_callback(TelnetConnection.__negotiate_naws)
72+
self._telnet.open(self._host, self._port, self._timeout)
7273

7374
self._read_until(b'Login: ')
7475
self._telnet.write((self._username + '\n').encode('UTF-8'))
7576
self._read_until(b'Password: ')
7677
self._telnet.write((self._password + '\n').encode('UTF-8'))
7778

7879
self._read_response(True)
80+
self._set_max_window_size()
7981
except Exception as e:
8082
message = "Error connecting to telnet server: %s" % str(e)
8183
_LOGGER.error(message)
@@ -105,30 +107,35 @@ def _read_until(self, needle: Union[bytes, Pattern]) -> (Match, bytes):
105107
assert i == 0, "No expected response from server"
106108
return match, text
107109

110+
# noinspection PyProtectedMember
111+
def _set_max_window_size(self):
112+
"""
113+
--> inform the Telnet server of the window width and height. see __negotiate_naws
114+
"""
115+
from telnetlib import IAC, NAWS, SB, SE
116+
import struct
117+
118+
width = struct.pack('H', 65000)
119+
height = struct.pack('H', 5000)
120+
self._telnet.get_socket().sendall(IAC + SB + NAWS + width + height + IAC + SE)
121+
108122
# noinspection PyProtectedMember
109123
@staticmethod
110-
def __set_max_window_size(tsocket, command, option):
124+
def __negotiate_naws(tsocket, command, option):
111125
"""
112-
Set Window size to resolve line width issue
113-
Set Windows size command: IAC SB NAWS <16-bit value> <16-bit value> IAC SE
114-
--> inform the Telnet server of the window width and height.
126+
--> inform the Telnet server we'll be using Window Size Option.
115127
Refer to https://www.ietf.org/rfc/rfc1073.txt
116128
:param tsocket: telnet socket object
117129
:param command: telnet Command
118130
:param option: telnet option
119131
:return: None
120132
"""
121-
from telnetlib import DO, DONT, IAC, WILL, WONT, NAWS, SB, SE
122-
import struct
133+
from telnetlib import DO, DONT, IAC, WILL, WONT, NAWS
123134

124135
if option == NAWS:
125-
width = struct.pack('H', 65000)
126-
height = struct.pack('H', 5000)
127-
tsocket.send(IAC + WILL + NAWS)
128-
tsocket.send(IAC + SB + NAWS + width + height + IAC + SE)
129-
# -- below code taken from telnetlib source
136+
tsocket.sendall(IAC + WILL + NAWS)
137+
# -- below code taken from telnetlib
130138
elif command in (DO, DONT):
131-
tsocket.send(IAC + WONT + option)
139+
tsocket.sendall(IAC + WONT + option)
132140
elif command in (WILL, WONT):
133-
tsocket.send(IAC + DONT + option)
134-
141+
tsocket.sendall(IAC + DONT + option)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="ndms2_client",
8-
version="0.1.1",
8+
version="0.1.2",
99
author="Andrey F. Kupreychik",
1010
author_email="foxel@quickfox.ru",
1111
description="Keenetic NDMS 2.x and 3.x client",

0 commit comments

Comments
 (0)