|
25 | 25 | import math
|
26 | 26 | import random
|
27 | 27 | import socket
|
28 |
| -import struct |
29 | 28 | import time
|
30 | 29 | import unittest
|
31 | 30 |
|
@@ -92,11 +91,11 @@ def ser_compact_size(l):
|
92 | 91 | if l < 253:
|
93 | 92 | r = l.to_bytes(1, "little")
|
94 | 93 | elif l < 0x10000:
|
95 |
| - r = struct.pack("<BH", 253, l) |
| 94 | + r = (253).to_bytes(1, "little") + l.to_bytes(2, "little") |
96 | 95 | elif l < 0x100000000:
|
97 |
| - r = struct.pack("<BI", 254, l) |
| 96 | + r = (254).to_bytes(1, "little") + l.to_bytes(4, "little") |
98 | 97 | else:
|
99 |
| - r = struct.pack("<BQ", 255, l) |
| 98 | + r = (255).to_bytes(1, "little") + l.to_bytes(8, "little") |
100 | 99 | return r
|
101 | 100 |
|
102 | 101 |
|
@@ -1635,12 +1634,12 @@ def __init__(self, announce=False, version=1):
|
1635 | 1634 | self.version = version
|
1636 | 1635 |
|
1637 | 1636 | def deserialize(self, f):
|
1638 |
| - self.announce = struct.unpack("<?", f.read(1))[0] |
| 1637 | + self.announce = bool(int.from_bytes(f.read(1), "little")) |
1639 | 1638 | self.version = int.from_bytes(f.read(8), "little")
|
1640 | 1639 |
|
1641 | 1640 | def serialize(self):
|
1642 | 1641 | r = b""
|
1643 |
| - r += struct.pack("<?", self.announce) |
| 1642 | + r += int(self.announce).to_bytes(1, "little") |
1644 | 1643 | r += self.version.to_bytes(8, "little")
|
1645 | 1644 | return r
|
1646 | 1645 |
|
|
0 commit comments