I am trying to convert a PHP code to python.
All values are send in network byte order (big endian).
Basically, the REQUEST in protocol specification is

and response is

Corresponding PHP Code (corresponding DOC) is:
$transaction_id = mt_rand(0,65535);
$current_connid = "\x00\x00\x04\x17\x27\x10\x19\x80";
$fp = fsockopen($tracker, $port, $errno, $errstr);
$packet = $current_connid . pack("N", 0) . pack("N", $transaction_id);
fwrite($fp,$packet);
I am trying to find the corresponding code (for doc) in python:
transaction_id = random.randrange(1,65535)
packet = "\x00\x00\x04\x17\x27\x10\x19\x80"
packet = packet + struct.pack("i", 0) + struct.pack("i", transaction_id)
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clisocket.sendto(packet, ("tracker.istole.it", 80))
In the response, I should get the same transaction_id I sent in the request which I am not getting. So, my guess is, I am not packing using correct format.
Also, the python documentation is not as clear as that of PHP. The protocol specifies to use Big Endian format & PHP doc clearly states which are the ones for Big-Endian.
Sadly, I could not comprehend which format to use in python. Please help me in choosing the corrent format.
EDIT:
Not getting any responses, so I would say more.
import struct
import socket
import random
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
packet = "\x00\x00\x04\x17\x27\x10\x19\x80"
transaction_id = random.randrange(1,65535)
print transaction_id
packet = packet+struct.pack(">i", 0)
packet = packet+struct.pack(">i", transaction_id)
clisocket.sendto(packet, ("tracker.istole.it", 80))
res = clisocket.recv(16)
print struct.unpack(">i", res[12:16])
According to protocol specification, I should be returned same INTEGER.
The php pack function format
Nmeans unsigned 32-bit big-endian integer.The corresponding Python
struct.packformat is>L.The images you posted for the protocol show the
connection_idshould be 64-bit (unsigned) integer: Python struct.pack formatQ.So: