vici: Add support for python 3
This commit is contained in:
parent
c7e3c5943f
commit
2e74aa0a91
|
@ -3,6 +3,7 @@ EXTRA_DIST = LICENSE MANIFEST.in \
|
|||
vici/test/__init__.py \
|
||||
vici/test/test_protocol.py \
|
||||
vici/__init__.py \
|
||||
vici/compat.py \
|
||||
vici/exception.py \
|
||||
vici/protocol.py \
|
||||
vici/session.py
|
||||
|
|
|
@ -25,6 +25,9 @@ setup(
|
|||
"License :: OSI Approved :: MIT License",
|
||||
"Natural Language :: English",
|
||||
"Programming Language :: Python :: 2.7",
|
||||
"Programming Language :: Python :: 3.2",
|
||||
"Programming Language :: Python :: 3.3",
|
||||
"Programming Language :: Python :: 3.4",
|
||||
"Topic :: Security",
|
||||
"Topic :: Software Development :: Libraries",
|
||||
)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
# Help functions for compatibility between python version 2 and 3
|
||||
|
||||
|
||||
# From http://legacy.python.org/dev/peps/pep-0469
|
||||
try:
|
||||
dict.iteritems
|
||||
except AttributeError:
|
||||
# python 3
|
||||
def iteritems(d):
|
||||
return iter(d.items())
|
||||
else:
|
||||
# python 2
|
||||
def iteritems(d):
|
||||
return d.iteritems()
|
|
@ -5,6 +5,7 @@ import struct
|
|||
from collections import namedtuple
|
||||
from collections import OrderedDict
|
||||
|
||||
from .compat import iteritems
|
||||
from .exception import DeserializationException
|
||||
|
||||
|
||||
|
@ -51,6 +52,7 @@ class Packet(object):
|
|||
|
||||
@classmethod
|
||||
def _named_request(cls, request_type, request, message=None):
|
||||
request = request.encode()
|
||||
payload = struct.pack("!BB", request_type, len(request)) + request
|
||||
if message is not None:
|
||||
return payload + message
|
||||
|
@ -93,22 +95,23 @@ class Message(object):
|
|||
@classmethod
|
||||
def serialize(cls, message):
|
||||
def encode_named_type(marker, name):
|
||||
name = str(name)
|
||||
name = name.encode()
|
||||
return struct.pack("!BB", marker, len(name)) + name
|
||||
|
||||
def encode_blob(value):
|
||||
value = str(value)
|
||||
if not isinstance(value, bytes):
|
||||
value = str(value).encode()
|
||||
return struct.pack("!H", len(value)) + value
|
||||
|
||||
def serialize_list(lst):
|
||||
segment = str()
|
||||
segment = bytes()
|
||||
for item in lst:
|
||||
segment += struct.pack("!B", cls.LIST_ITEM) + encode_blob(item)
|
||||
return segment
|
||||
|
||||
def serialize_dict(d):
|
||||
segment = str()
|
||||
for key, value in d.iteritems():
|
||||
segment = bytes()
|
||||
for key, value in iteritems(d):
|
||||
if isinstance(value, dict):
|
||||
segment += (
|
||||
encode_named_type(cls.SECTION_START, key)
|
||||
|
@ -134,7 +137,7 @@ class Message(object):
|
|||
def deserialize(cls, stream):
|
||||
def decode_named_type(stream):
|
||||
length, = struct.unpack("!B", stream.read(1))
|
||||
return stream.read(length)
|
||||
return stream.read(length).decode()
|
||||
|
||||
def decode_blob(stream):
|
||||
length, = struct.unpack("!H", stream.read(2))
|
||||
|
|
|
@ -241,7 +241,7 @@ class SessionHandler(object):
|
|||
|
||||
command_response = Message.deserialize(response.payload)
|
||||
if "success" in command_response:
|
||||
if command_response["success"] != "yes":
|
||||
if command_response["success"] != b"yes":
|
||||
raise CommandException(
|
||||
"Command failed: {errmsg}".format(
|
||||
errmsg=command_response["errmsg"]
|
||||
|
@ -319,7 +319,7 @@ class SessionHandler(object):
|
|||
|
||||
# evaluate command result, if any
|
||||
if "success" in command_response:
|
||||
if command_response["success"] != "yes":
|
||||
if command_response["success"] != b"yes":
|
||||
raise CommandException(
|
||||
"Command failed: {errmsg}".format(
|
||||
errmsg=command_response["errmsg"]
|
||||
|
|
Loading…
Reference in New Issue