I have bytes in Python: b'P\xf3g9\xea\xc3'
I know that this is an IP address and I know how to convert it to one (this is based on BitTorrent specificationBitTorrent specification -> the peers"the peers value may be a string consisting of multiples of 6 bytes. First 4 bytes are the IP address and last 2 bytes are the port number. All in network (big endian) notation url."):
import socket import struct p = `b'P\xf3g9\xea\xc3'`b'P\xf3g9\xea\xc3' print(f'peer {p}: bytes IP: {p[:4]}; bytes PORT: {p[4:]}') >>># > peer b'P\xf3g9\xea\xc3': bytes IP: b'P\xf3g9'; bytes PORT: b'\xea\xc3' print('IP', socket.inet_ntoa(p[:4])) >>># -> IP 80.243.103.57 # > - big-endian; H - unsigned short print('PORT', struct.unpack(">H", p[4:])[0]) >>># -> PORT 60099 I'm struggling to understand how this works? Ideally I would like to understand how to convert bytes to the IP address by hand.
Some questions that I have:
- what is the leading
Pin bytes? - how to transform this represntation (i assume it's hex?) to base 2?
- I think I understand
\xf3-> that translates to243. What abaoutg9?
Would be great if some one could explain or point somewhere where I can read more about it.