From 3c9066fce54b78cc8b46e82eba033aaa373cdef1 Mon Sep 17 00:00:00 2001 From: Marc Hoersken Date: Tue, 17 Mar 2020 10:36:25 +0100 Subject: 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 --- tests/negtelnetserver.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'tests/negtelnetserver.py') 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, , 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) -- cgit v1.2.3