From 01327f36b7131e11f2abc399f7539a80fb3a640a Mon Sep 17 00:00:00 2001 From: Mattias Loverot Date: Wed, 22 Jun 2016 14:10:15 +0200 Subject: [PATCH 1/2] Python 3 support --- varnish.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/varnish.py b/varnish.py index 148010f..24bea26 100644 --- a/varnish.py +++ b/varnish.py @@ -33,11 +33,18 @@ """ from telnetlib import Telnet from threading import Thread -from httplib import HTTPConnection -from urlparse import urlparse from hashlib import sha256 import logging +try: + from httplib import HTTPConnection +except ImportError: + from http.client import HTTPConnection # py3 + +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse logging.basicConfig( level = logging.DEBUG, @@ -61,7 +68,7 @@ def http_purge_url(url): class VarnishHandler(Telnet): def __init__(self, host_port_timeout, secret=None, **kwargs): - if isinstance(host_port_timeout, basestring): + if isinstance(host_port_timeout, str): host_port_timeout = host_port_timeout.split(':') if (len(host_port_timeout) == 3): @@ -75,7 +82,7 @@ def __init__(self, host_port_timeout, secret=None, **kwargs): logging.error('Connecting failed with status: %i' % status) def _read(self): - (status, length), content = map(int, self.read_until('\n').split()), '' + (status, length), content = list(map(int, self.read_until('\n').split())), '' while len(content) < length: content += self.read_some() return (status, length), content[:-1] @@ -91,7 +98,7 @@ def fetch(self, command): buffer = self.read_until('\n').strip() if len(buffer): break - status, length = map(int, buffer.split()) + status, length = list(map(int, buffer.split())) content = '' assert status == 200, 'Bad response code: {status} {text} ({command})'.format(status=status, text=self.read_until('\n').strip(), command=command) while len(content) < length: From 3a534d2e9f74cbe57248855bd91ed54b1f933ad8 Mon Sep 17 00:00:00 2001 From: Mattias Loverot Date: Wed, 22 Jun 2016 16:48:13 +0200 Subject: [PATCH 2/2] Requires byte --- varnish.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/varnish.py b/varnish.py index 24bea26..1e565d1 100644 --- a/varnish.py +++ b/varnish.py @@ -82,7 +82,7 @@ def __init__(self, host_port_timeout, secret=None, **kwargs): logging.error('Connecting failed with status: %i' % status) def _read(self): - (status, length), content = list(map(int, self.read_until('\n').split())), '' + (status, length), content = list(map(int, self.read_until(b'\n').split())), b'' while len(content) < length: content += self.read_some() return (status, length), content[:-1] @@ -93,16 +93,18 @@ def fetch(self, command): return value is a tuple of ((status, length), content) """ logging.debug('SENT: %s: %s' % (self.host, command)) - self.write('%s\n' % command) + self.write(b'%s\n' % bytes(command, 'utf8')) while 1: - buffer = self.read_until('\n').strip() + buffer = self.read_until(b'\n').strip() if len(buffer): break status, length = list(map(int, buffer.split())) - content = '' - assert status == 200, 'Bad response code: {status} {text} ({command})'.format(status=status, text=self.read_until('\n').strip(), command=command) + content = b'' + assert status == 200, 'Bad response code: {status} {text} ({command})'.format(status=status, text=self.read_until(b'\n').strip(), command=command) while len(content) < length: - content += self.read_until('\n') + content += self.read_until(b'\n') + + content = content.decode('utf8') logging.debug('RECV: %s: %dB %s' % (status,length,content[:30])) self.read_eager() return (status, length), content