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
This commit is contained in:
Marc Hoersken 2020-03-17 10:36:25 +01:00
parent 8d9802b0ae
commit 3c9066fce5
No known key found for this signature in database
GPG key ID: 61E03CBED7BC859E
7 changed files with 59 additions and 24 deletions

View file

@ -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)