From e3a301a85d355a7f559173b88613fb4da5fbd016 Mon Sep 17 00:00:00 2001 From: Andrew Lewis Date: Wed, 29 Jun 2016 17:43:16 +0200 Subject: [PATCH] [Test] Fix Python3 compatibility --- test/functional/lib/rspamd.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/test/functional/lib/rspamd.py b/test/functional/lib/rspamd.py index 998eb6960..93ec4c430 100644 --- a/test/functional/lib/rspamd.py +++ b/test/functional/lib/rspamd.py @@ -6,9 +6,16 @@ import shutil import signal import socket import string +import sys import tempfile import time -import urllib2 + +if sys.version_info > (3,): + long = int +try: + from urllib.request import urlopen +except: + from urllib2 import urlopen def cleanup_temporary_directory(directory): shutil.rmtree(directory) @@ -41,9 +48,7 @@ def read_log_from_position(filename, offset): return [goo, size+offset] def scan_file(addr, port, filename): - req = urllib2.Request("http://%s:%s/symbols?%s" % (addr, port, filename)) - response = urllib2.urlopen(req) - return response.read() + return str(urlopen("http://%s:%s/symbols?%s" % (addr, port, filename)).read()) def Send_SIGUSR1(pid): pid = int(pid) @@ -56,13 +61,15 @@ def set_directory_ownership(path, username, groupname): def spamc(addr, port, filename): goo = open(filename, 'rb').read() - length = len(goo) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((addr, port)) - s.send("SYMBOLS SPAMC/1.0\r\nContent-length: %s\r\n\r\n%s" % (length, goo)) + s.send(b"SYMBOLS SPAMC/1.0\r\nContent-length: ") + s.send(str(len(goo)).encode('utf-8')) + s.send(b"\r\n\r\n") + s.send(goo) s.shutdown(socket.SHUT_WR) r = s.recv(2048) - return r + return r.decode('utf-8') def update_dictionary(a, b): a.update(b) -- 2.39.5