Hey folks. I’m stuck on #ND2 and would like some help.
I’m currently on the step for receiving the unchoke
message, right after sending the interested
message. I’m not getting any data back from the peer though, and tests time out.
FYI I’m doing a sequential implementation for starters.
Here’s the relevant piece of code:
peers = get_peers(torrent_filename)
peer_id, reader, writer = await perform_handshake(
torrent_filename, peer=peers[0]
)
# Receive bitfield message
bitfield_size = int.from_bytes(await reader.readexactly(4))
bitfield_type = int.from_bytes(await reader.readexactly(1))
if bitfield_type != 0x05:
raise Exception("Expected bitfield type")
_bitfield_payload = await reader.read(bitfield_size)
# Send interested message
# Payload size is 0, interested type is 2
message = bytes().join(int.to_bytes(0) for _ in range(4)) + int.to_bytes(2)
writer.write(message)
await writer.drain()
# Receive unchoke message
unchoke_size = int.from_bytes(await reader.readexactly(4))
unchoke_type = int.from_bytes(await reader.readexactly(1))
if unchoke_type != 0x01 or unchoke_size != 0x00:
raise Exception("Expected unchoke type with 0 size")
print("foo!")
And here is the repo with all of the code: