【问题标题】:bittorrent udp tracker responsebittorrent udp 跟踪器响应
【发布时间】:2012-01-27 08:21:34
【问题描述】:

我怀疑如何按照 bep udp 协议规范中的建议处理 udp 宣布响应

import socket,struct
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
connection_id=0x41727101980
transaction_id = 12345

# responses Tracker : Specification  UDP [ User Datagaram Protocol ]
info_hash  = "%1D%D4%D1%EDQn%DB%5CL%83%90%1B%2B%F8%83%A2%19%C0%7C%98"
peer_id = "-UT1234-m%09%B2%D5%99%FA%1Fj%88%AC%0D%A7"
action =1 # announce
downloaded = 0
left = 0
uploaded = 0
event =0
ip = 0
key = 0
num_want = -1
port = 9999

announce_pack = struct.pack(">QLL20s20sQQQLLLLH",connection_id,action,transaction_id,info_hash,peer_id,down  loaded,left,uploaded,event,ip,key,num_want,port)
clisocket.sendto(announce_pack, ("tracker.ccc.de", 80))
res = clisocket.recv(1024)
action=struct.unpack(">HLLLLQQQ20s20sLLQ",res)

但我收到以下错误

DeprecationWarning: struct integer overflow masking is deprecated announce_pack=struct.pack(">QLL20s20sQQQLLLLH",connection_id,action,transaction_id,info_hash,peer_id,downloaded,left,uploaded,event,ip,key,num_want,port)
Traceback (most recent call last):
action=struct.unpack(">HLLLLQQQ20s20sLLQ",res)
struct.error: unpack requires a string argument of length 98

【问题讨论】:

    标签: python udp bittorrent tracker utorrent


    【解决方案1】:

    看起来您正在向 struct 传递一个太大的字符串,可能是因为您收到了多个数据包。试试

    action=struct.unpack(">HLLLLQQQ20s20sLLQ",res[:98])
    

    此外,您似乎没有正确解析宣布响应。 UDP 跟踪数据包的格式见here

    【讨论】:

    • 嗨 Guillaume,我对你的结构格式有点困惑。根据 UDP 跟踪器规范,格式掩码不会像 >LLLL... 这样的东西吗?您能否详细说明一下,因为我是结构新手。谢谢。
    • Mridang,我认为你是对的。查看规范,对于后面的 N 个主机,格式应该是“>LLLLL”,然后是“>LHLHLH...”。 (当我说“您似乎没有正确解析宣布响应”时,这就是我的意思。我只是按照原始帖子的格式工作,这需要长度为 98 的字符串。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多