mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-08-31 14:05:33 +00:00
Python 2 to 3 conversions
git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@166 e5f2f494-b856-4b98-b285-d166d9295462
This commit is contained in:
@@ -13,6 +13,9 @@
|
|||||||
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||||
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
# XXX
|
||||||
|
from ISC.Util import hexdump
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
@@ -35,16 +38,13 @@ def to_wire(items):
|
|||||||
"""Encode a dict into wire format.
|
"""Encode a dict into wire format.
|
||||||
>>> wire_format = Message.to_wire({"a": "b"})
|
>>> wire_format = Message.to_wire({"a": "b"})
|
||||||
"""
|
"""
|
||||||
ret = []
|
return struct.pack(">I", PROTOCOL_VERSION) + _encode_hash(items)
|
||||||
ret.append(struct.pack(">I", PROTOCOL_VERSION))
|
|
||||||
ret.append(_encode_hash(items))
|
|
||||||
return (''.join(ret))
|
|
||||||
|
|
||||||
def _encode_tag(tag):
|
def _encode_tag(tag):
|
||||||
"""Encode a single UTF-8 tag.
|
"""Encode a single UTF-8 tag.
|
||||||
... wire_partial = Message._encode_tag('this')
|
... wire_partial = Message._encode_tag('this')
|
||||||
"""
|
"""
|
||||||
return(struct.pack(">B", len(str(tag))) + str(tag))
|
return(struct.pack(">B", len(tag)) + bytearray(tag, 'utf-8'))
|
||||||
|
|
||||||
def _encode_length_and_type(data, datatype):
|
def _encode_length_and_type(data, datatype):
|
||||||
"""Helper method to handle the length encoding in one place."""
|
"""Helper method to handle the length encoding in one place."""
|
||||||
@@ -52,22 +52,19 @@ def _encode_length_and_type(data, datatype):
|
|||||||
return(struct.pack(">B", _ITEM_NULL))
|
return(struct.pack(">B", _ITEM_NULL))
|
||||||
length = len(data)
|
length = len(data)
|
||||||
if length < 0x0000100:
|
if length < 0x0000100:
|
||||||
return(struct.pack(">B B", datatype | _ITEM_LENGTH_8, length)
|
return(struct.pack(">B B", datatype | _ITEM_LENGTH_8, length) + data)
|
||||||
+ data)
|
|
||||||
elif length < 0x00010000:
|
elif length < 0x00010000:
|
||||||
return(struct.pack(">B H", datatype | _ITEM_LENGTH_16, length)
|
return(struct.pack(">B H", datatype | _ITEM_LENGTH_16, length) + data)
|
||||||
+ data)
|
|
||||||
else:
|
else:
|
||||||
return(struct.pack(">B I", datatype, length) + data)
|
return(struct.pack(">B I", datatype, length) + data)
|
||||||
|
|
||||||
def _pack_string(item):
|
def _pack_string(item):
|
||||||
"""Pack a string (data) and its type/length prefix."""
|
"""Pack a string (data) and its type/length prefix."""
|
||||||
return (_encode_length_and_type(item, _ITEM_DATA))
|
return (_encode_length_and_type(bytearray(item, 'utf-8'), _ITEM_DATA))
|
||||||
|
|
||||||
def _pack_array(item):
|
def _pack_array(item):
|
||||||
"""Pack a list (array) and its type/length prefix."""
|
"""Pack a list (array) and its type/length prefix."""
|
||||||
return (_encode_length_and_type(_encode_array(item),
|
return (_encode_length_and_type(_encode_array(item), _ITEM_LIST))
|
||||||
_ITEM_LIST))
|
|
||||||
|
|
||||||
def _pack_hash(item):
|
def _pack_hash(item):
|
||||||
"""Pack a dict (hash) and its type/length prefix."""
|
"""Pack a dict (hash) and its type/length prefix."""
|
||||||
@@ -90,24 +87,26 @@ def _encode_item(item):
|
|||||||
return (_pack_hash(item))
|
return (_pack_hash(item))
|
||||||
elif type(item) == list:
|
elif type(item) == list:
|
||||||
return (_pack_array(item))
|
return (_pack_array(item))
|
||||||
|
elif type(item) in (bytearray, bytes):
|
||||||
|
return (_pack_string(item.decode()))
|
||||||
else:
|
else:
|
||||||
return (_pack_string(str(item)))
|
return (_pack_string(str(item)))
|
||||||
|
|
||||||
def _encode_array(item):
|
def _encode_array(item):
|
||||||
"""Encode an array, where each value is encoded recursively"""
|
"""Encode an array, where each value is encoded recursively"""
|
||||||
ret = []
|
ret = bytearray()
|
||||||
for i in item:
|
for i in item:
|
||||||
ret.append(_encode_item(i))
|
ret += _encode_item(i)
|
||||||
return (''.join(ret))
|
return ret
|
||||||
|
|
||||||
def _encode_hash(item):
|
def _encode_hash(item):
|
||||||
"""Encode a hash, where each value is encoded recursively"""
|
"""Encode a hash, where each value is encoded recursively"""
|
||||||
|
|
||||||
ret = []
|
ret = bytearray()
|
||||||
for key, value in item.items():
|
for key, value in item.items():
|
||||||
ret.append(_encode_tag(key))
|
ret += _encode_tag(key)
|
||||||
ret.append(_encode_item(value))
|
ret += _encode_item(value)
|
||||||
return (''.join(ret))
|
return ret
|
||||||
|
|
||||||
#
|
#
|
||||||
# decode methods
|
# decode methods
|
||||||
@@ -125,15 +124,15 @@ def from_wire(data):
|
|||||||
def _decode_tag(data):
|
def _decode_tag(data):
|
||||||
if len(data) < 1:
|
if len(data) < 1:
|
||||||
raise DecodeError("Data underrun while decoding")
|
raise DecodeError("Data underrun while decoding")
|
||||||
length = struct.unpack(">B", data[0])[0]
|
length = data[0]
|
||||||
if len(data) - 1 < length:
|
if len(data) - 1 < length:
|
||||||
raise DecodeError("Data underrun while decoding")
|
raise DecodeError("Data underrun while decoding")
|
||||||
return [data[1:length + 1], data[length + 1:]]
|
return [data[1:length + 1].decode(), data[length + 1:]]
|
||||||
|
|
||||||
def _decode_item(data):
|
def _decode_item(data):
|
||||||
if len(data) < 1:
|
if len(data) < 1:
|
||||||
raise DecodeError("Data underrun while decoding")
|
raise DecodeError("Data underrun while decoding")
|
||||||
type_and_length_format = struct.unpack(">B", data[0])[0]
|
type_and_length_format = data[0]
|
||||||
item_type = type_and_length_format & _ITEM_MASK
|
item_type = type_and_length_format & _ITEM_MASK
|
||||||
length_format = type_and_length_format & _ITEM_LENGTH_MASK
|
length_format = type_and_length_format & _ITEM_LENGTH_MASK
|
||||||
|
|
||||||
@@ -143,7 +142,7 @@ def _decode_item(data):
|
|||||||
if length_format == _ITEM_LENGTH_8:
|
if length_format == _ITEM_LENGTH_8:
|
||||||
if len(data) - 1 < 1:
|
if len(data) - 1 < 1:
|
||||||
raise DecodeError("Data underrun while decoding")
|
raise DecodeError("Data underrun while decoding")
|
||||||
length = struct.unpack(">B", data[1])[0]
|
length = data[1]
|
||||||
data = data[2:]
|
data = data[2:]
|
||||||
elif length_format == _ITEM_LENGTH_16:
|
elif length_format == _ITEM_LENGTH_16:
|
||||||
if len(data) - 1 < 2:
|
if len(data) - 1 < 2:
|
||||||
|
@@ -13,6 +13,10 @@
|
|||||||
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
||||||
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
# XXX
|
||||||
|
import pprint
|
||||||
|
from ISC.Util import hexdump
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
@@ -27,9 +31,9 @@ class Session:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._socket = None
|
self._socket = None
|
||||||
self._lname = None
|
self._lname = None
|
||||||
self._recvbuffer = ""
|
self._recvbuffer = bytearray()
|
||||||
self._recvlength = None
|
self._recvlength = None
|
||||||
self._sendbuffer = ""
|
self._sendbuffer = bytearray()
|
||||||
self._sequence = 1
|
self._sequence = 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -81,7 +85,7 @@ class Session:
|
|||||||
if len(self._recvbuffer) < 4:
|
if len(self._recvbuffer) < 4:
|
||||||
return None
|
return None
|
||||||
self._recvlength = struct.unpack('>I', self._recvbuffer)[0]
|
self._recvlength = struct.unpack('>I', self._recvbuffer)[0]
|
||||||
self._recvbuffer = ""
|
self._recvbuffer = bytearray()
|
||||||
|
|
||||||
length = self._recvlength - len(self._recvbuffer)
|
length = self._recvlength - len(self._recvbuffer)
|
||||||
while (length > 0):
|
while (length > 0):
|
||||||
@@ -94,7 +98,7 @@ class Session:
|
|||||||
self._recvbuffer += data
|
self._recvbuffer += data
|
||||||
length -= len(data)
|
length -= len(data)
|
||||||
data = self._recvbuffer
|
data = self._recvbuffer
|
||||||
self._recvbuffer = ""
|
self._recvbuffer = bytearray()
|
||||||
self._recvlength = None
|
self._recvlength = None
|
||||||
return (data)
|
return (data)
|
||||||
|
|
||||||
|
@@ -4,10 +4,10 @@ _FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
|
|||||||
|
|
||||||
def hexdump(src, length=16):
|
def hexdump(src, length=16):
|
||||||
result=[]
|
result=[]
|
||||||
for i in xrange(0, len(src), length):
|
for i in range(0, len(src), length):
|
||||||
s = src[i:i+length]
|
s = src[i:i+length]
|
||||||
hexa = ' '.join(["%02X" % ord(x) for x in s])
|
hexa = ' '.join(["%02X" % x for x in s])
|
||||||
printable = s.translate(_FILTER)
|
printable = s.decode().translate(_FILTER)
|
||||||
item = "%08X %-*s %s\n" % (i, length * 3, hexa, printable)
|
item = "%08X %-*s %s\n" % (i, length * 3, hexa, printable)
|
||||||
result.append(item[0:34] + ' ' + item[34:])
|
result.append(item[0:34] + ' ' + item[34:])
|
||||||
print(''.join(result))
|
print(''.join(result))
|
||||||
|
@@ -8,10 +8,10 @@ ss = { "list": [ 1, 2, 3 ],
|
|||||||
s = ISC.CC.Message.to_wire(ss)
|
s = ISC.CC.Message.to_wire(ss)
|
||||||
ISC.Util.hexdump(s)
|
ISC.Util.hexdump(s)
|
||||||
|
|
||||||
print ISC.CC.Message.from_wire(s)
|
print(ISC.CC.Message.from_wire(s))
|
||||||
|
|
||||||
tcp = ISC.CC.Session()
|
tcp = ISC.CC.Session()
|
||||||
print tcp.lname
|
print(tcp.lname)
|
||||||
|
|
||||||
tcp.group_subscribe("test")
|
tcp.group_subscribe("test")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user