Bittorrent Cien没有传入对等连接

2024-05-16 00:56:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python和twisted为流媒体文件创建一个Bittorrent客户端,并成功地做到了这一点。在

我使用Twisted框架来连接跟踪器和对等点。我可以从跟踪器中检索对等方列表,并通过对每个对等方执行reactor.connectTCP()函数来连接到它们。在

但是我的客户端无法监听TCP连接。意味着没有来自对等端的传入连接。我在一个给定的端口使用reactor.listenTCP()。我在请求数据包中使用的同一个端口,我正在向跟踪器发送公告请求。 我发送到跟踪器的请求包生成如下:

def udp_create_announce_request(connection_id, payload, s_port):
    action = 0x1 #action (1 = announce)
    transaction_id = udp_get_transaction_id()
    buf = struct.pack("!q", connection_id)                                  #first 8 bytes is connection id
    buf += struct.pack("!i", action)                                        #next 4 bytes is action 
    buf += struct.pack("!i", transaction_id)                                #followed by 4 byte transaction id
    buf += struct.pack("!20s", urllib.unquote(payload['info_hash']))        #the info hash of the torrent we announce ourselves in
    buf += struct.pack("!20s", urllib.unquote(payload['peer_id']))          #the peer_id we announce
    buf += struct.pack("!q", int(urllib.unquote(payload['downloaded'])))    #number of bytes downloaded
    buf += struct.pack("!q", int(urllib.unquote(payload['left'])))          #number of bytes left
    buf += struct.pack("!q", int(urllib.unquote(payload['uploaded'])))      #number of bytes uploaded
    buf += struct.pack("!i", 0x2)                                           #event 2 denotes start of downloading
    buf += struct.pack("!i", 0x0)                                           #IP address set to 0. Response received to the sender of this packet. IP decided by tracker. 
    key = udp_get_transaction_id()                                          #Unique key randomized by client
    buf += struct.pack("!i", key)
    buf += struct.pack("!i", -1)                                            #Number of peers required. Set to -1 for default
    buf += struct.pack("!i", int(urllib.unquote(payload['port'])))          #port on which response will be sent
return (buf, transaction_id)

创建数据包时是否出错?或者没有同龄人尝试连接到我的客户机端口有不同的原因吗?工厂中的协议可能有问题吗?在

编辑

反应器调用顺序如下:

^{2}$

Echo协议定义为:

class Echo(protocol.Protocol):
    """This is just about the simplest possible protocol""" 
    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print "Hello", data 
        # Never Printed

{PORT也显示监听端口^正确


Tags: ofthe端口idbytesisactionurllib