aboutsummaryrefslogtreecommitdiff
path: root/tests/negtelnetserver.py
diff options
context:
space:
mode:
authorMarc Hoersken <info@marc-hoersken.de>2020-03-17 10:36:25 +0100
committerMarc Hoersken <info@marc-hoersken.de>2020-03-19 03:26:19 +0100
commit3c9066fce54b78cc8b46e82eba033aaa373cdef1 (patch)
tree25620b454e007eb89162bf9437cfaadb2bdf5db4 /tests/negtelnetserver.py
parent8d9802b0aed12932612847f4267a42d08b7bfa71 (diff)
tests: make Python-based servers compatible with Python 2 and 3
Update smbserver.py and negtelnetserver.py to be compatible with Python 3 while staying backwards-compatible to support Python 2. Fix string encoding and handling of echoed and transferred data. Tested with both Python 2.7.17 and Python 3.7.7 Reported-by: Daniel Stenberg Assisted-by: Kamil Dudka Reviewed-by: Marcel Raad Fixes #5104 Closes #5110
Diffstat (limited to 'tests/negtelnetserver.py')
-rwxr-xr-xtests/negtelnetserver.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/tests/negtelnetserver.py b/tests/negtelnetserver.py
index f2f2ab500..1efc64b56 100755
--- a/tests/negtelnetserver.py
+++ b/tests/negtelnetserver.py
@@ -1,6 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
+# Project ___| | | | _ \| |
+# / __| | | | |_) | |
+# | (__| |_| | _ <| |___
+# \___|\___/|_| \_\_____|
+#
+# Copyright (C) 2017 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+#
+# This software is licensed as described in the file COPYING, which
+# you should have received as part of this distribution. The terms
+# are also available at https://curl.haxx.se/docs/copyright.html.
+#
+# You may opt to use, copy, modify, merge, publish, distribute and/or sell
+# copies of the Software, and permit persons to whom the Software is
+# furnished to do so, under the terms of the COPYING file.
+#
+# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+# KIND, either express or implied.
+#
""" A telnet server which negotiates"""
from __future__ import (absolute_import, division, print_function,
@@ -9,11 +27,10 @@ import argparse
import os
import sys
import logging
-try: # Python 2
- import SocketServer as socketserver
-except ImportError: # Python 3
+if sys.version_info.major >= 3:
import socketserver
-
+else:
+ import SocketServer as socketserver
log = logging.getLogger(__name__)
HOST = "localhost"
@@ -67,13 +84,13 @@ class NegotiatingTelnetHandler(socketserver.BaseRequestHandler):
data = neg.recv(1024)
log.debug("Incoming data: %r", data)
- if VERIFIED_REQ.encode('ascii') in data:
+ if VERIFIED_REQ.encode('utf-8') in data:
log.debug("Received verification request from test framework")
response = VERIFIED_RSP.format(pid=os.getpid())
- response_data = response.encode('ascii')
+ response_data = response.encode('utf-8')
else:
log.debug("Received normal request - echoing back")
- response_data = data.strip()
+ response_data = data.decode('utf-8').strip().encode('utf-8')
if response_data:
log.debug("Sending %r", response_data)