Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 support #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions varnish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand All @@ -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(b'\n').split())), b''
while len(content) < length:
content += self.read_some()
return (status, length), content[:-1]
Expand All @@ -86,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 = 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)
status, length = list(map(int, buffer.split()))
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
Expand Down