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

适配python3 #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions beanstalkc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import socket
import sys

__python_version__ = sys.version_info.major


__license__ = '''
Copyright (C) 2008-2016 Andreas Bolka
Expand Down Expand Up @@ -47,12 +49,20 @@ def wrap(wrapped_function, *args, **kwargs):
raise SocketError(err)


def to_str(data):
return data if isinstance(data, str) else data.decode()


def to_bytes(data):
return data if isinstance(data, bytes) else data.encode()


class Connection(object):
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, parse_yaml=True,
connect_timeout=socket.getdefaulttimeout()):
if parse_yaml is True:
try:
parse_yaml = __import__('yaml').load
parse_yaml = __import__('yaml').full_load
except ImportError:
logging.error('Failed to load PyYAML, will not parse YAML')
parse_yaml = False
Expand Down Expand Up @@ -93,6 +103,8 @@ def reconnect(self):
self.connect()

def _interact(self, command, expected_ok, expected_err=[]):

command = to_bytes(command) if __python_version__ == 3 else to_str(command)
SocketError.wrap(self._socket.sendall, command)
status, results = self._read_response()
if status in expected_ok:
Expand All @@ -106,6 +118,8 @@ def _read_response(self):
line = SocketError.wrap(self._socket_file.readline)
if not line:
raise SocketError()

line = to_str(line)
response = line.split()
return response[0], response[1:]

Expand All @@ -114,7 +128,7 @@ def _read_body(self, size):
SocketError.wrap(self._socket_file.read, 2) # trailing crlf
if size > 0 and not body:
raise SocketError()
return body
return to_str(body)

def _interact_value(self, command, expected_ok, expected_err=[]):
return self._interact(command, expected_ok, expected_err)[0]
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
''',
url='http://github.com/earl/beanstalkc',
license='Apache License, Version 2.0',
install_requires=[
"PyYAML==5.1.2"
],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down