示例#1
0
    def test_request(self):
        torrent = bt.TorrentFile('torrents/flagfromserver.torrent')
        received_file = open('testfile.jpg', 'rwb')
        controller = bt.Controller(torrent, received_file)

        incoming_request = message.generate_message('request', index=1, begin=0, length=2**14, peer_id='12345123451234512345')
        received_file.seek(torrent.piece_length * 1)
示例#2
0
文件: bt.py 项目: phsteve/bt
    def handle_handshake(self, bytes, messages, buff):
        pstr = bytes[1:20]
        reserved = bytes[20:28]
        info_hash = bytes[28:48]
        peer_id = bytes[48:68]
        # implement peer_id/info_hash checking
        # else:
        #need to check if peer already shook hands
        received_handshake = Handshake(info_hash,
                                       pstr=pstr,
                                       reserved=reserved,
                                       peer_id=peer_id)
        self.factory.peer.peer_id = peer_id
        # self.controller.peer_dict[peer_id].protocol = self
        self.controller.add_peer(self.factory.peer)
        self.shook_hands = True
        self.controller.peer_dict[peer_id].handshake = received_handshake
        print 'handshake received from: %r' % self.transport.getPeer()

        inter = generate_message('interested')
        print 'sent an interested to %r' % (self.transport.getPeer())
        self.transport.write(inter.bytes)
        self.controller.peer_dict[peer_id].status['am_interested'] = 1
        buff = buff[68:]
        messages, buff = Message.split_message(buff, peer_id)
        return messages, buff
示例#3
0
 def test_multiple_messages(self):
     pieces = [
         message.generate_message("piece", index=i, begin=0, block=struct.pack(">i", i) * 2 ** 8) for i in range(3)
     ]
     buff = "".join(p.bytes for p in pieces)
     messages, buff = message.Message.split_message(buff, "x" * 20)
     self.assertTrue(messages, pieces)
示例#4
0
文件: piece_test.py 项目: phsteve/bt
 def test_piece_generation(self):
     msg = message.generate_message('piece',
                                    index=0,
                                    begin=0,
                                    block=struct.pack('>i', 2) * (2**14))
     self.assertEquals(msg.index, 0)
     self.assertEquals(msg.message_len, 9 + msg.block_len)
     self.assertEquals(msg.begin, 0)
示例#5
0
文件: bt.py 项目: phsteve/bt
 def request_handler(self, req):
     # <len=0013><id=6><index><begin><length>
     self.received_file.seek(self.torrent.piece_length * req.index +
                             req.begin)
     piece = generate_message('piece',
                              index=req.index,
                              begin=req.begin,
                              block=self.received_file.read(req.length))
     self.peer_dict[req.peer_id].factory.transport.write(piece.bytes)
示例#6
0
 def test_partial_messages(self):
     piece = message.generate_message("piece", index=0, begin=0, block=struct.pack(">i", 1) * 2 ** 8)
     partial1 = piece.bytes[: piece.message_len / 3]
     partial2 = piece.bytes[piece.message_len / 3 :]
     buff = partial1
     messages, buff = message.Message.split_message(buff, "x" * 20)
     # receive 2nd message
     buff += partial2
     messages, buff = message.Message.split_message(buff, "x" * 20)
     self.assertTrue(messages, [piece])
示例#7
0
 def test_multiple_messages(self):
     pieces = [
         message.generate_message('piece',
                                  index=i,
                                  begin=0,
                                  block=struct.pack('>i', i) * 2**8)
         for i in range(3)
     ]
     buff = ''.join(p.bytes for p in pieces)
     messages, buff = message.Message.split_message(buff, 'x' * 20)
     self.assertTrue(messages, pieces)
示例#8
0
文件: bt_test.py 项目: phsteve/bt
    def test_piece(self):
        # '''<len=0009+X><id=7><index><begin><block>'''
        buff = StringIO.StringIO()
        torrent = bt.TorrentFile('torrents/flagfromserver.torrent')
        controller = bt.Controller(torrent, buff)
        for i in range(20):
            piece = message.generate_message('piece', index=i, begin=0, block=struct.pack('>i', i)*(2**8))
            controller.piece_handler(piece)
        # print 'buffer value: ' + repr(buff.getvalue())

        # self.assertEquals(len(buff.getvalue()), 2**17)
        expected_result = ''.join([struct.pack('>i', i)*2**8 for i in range(20)])
        self.assertEquals(buff.getvalue(), expected_result)
示例#9
0
 def test_partial_messages(self):
     piece = message.generate_message('piece',
                                      index=0,
                                      begin=0,
                                      block=struct.pack('>i', 1) * 2**8)
     partial1 = piece.bytes[:piece.message_len / 3]
     partial2 = piece.bytes[piece.message_len / 3:]
     buff = partial1
     messages, buff = message.Message.split_message(buff, 'x' * 20)
     #receive 2nd message
     buff += partial2
     messages, buff = message.Message.split_message(buff, 'x' * 20)
     self.assertTrue(messages, [piece])
示例#10
0
文件: bt.py 项目: phsteve/bt
 def handle_handshake(self, bytes, messages, buff):
     pstr = bytes[1:20]
     reserved = bytes[20:28]
     info_hash = bytes[28:48]
     peer_id = bytes[48:68]
     # implement peer_id/info_hash checking
     # else:
         #need to check if peer already shook hands
     received_handshake = Handshake(info_hash, pstr=pstr, reserved=reserved,
                                    peer_id=peer_id)
     self.factory.peer.peer_id = peer_id
     # self.controller.peer_dict[peer_id].protocol = self
     self.controller.add_peer(self.factory.peer)
     self.shook_hands = True
     self.controller.peer_dict[peer_id].handshake = received_handshake
     print 'handshake received from: %r' %self.transport.getPeer()
 
     inter = generate_message('interested')
     print 'sent an interested to %r' %(self.transport.getPeer())
     self.transport.write(inter.bytes)
     self.controller.peer_dict[peer_id].status['am_interested'] = 1
     buff = buff[68:]
     messages, buff = Message.split_message(buff, peer_id)
     return messages, buff
示例#11
0
文件: scratch.py 项目: phsteve/bt
# class Handshake(object):
#     # should this inherit from Message?
#     def __init__(self, peer, pstr='', pstrlen='', reserved='', info_hash='', peer_id=''):
#         self.peer = peer
#         self.pstr = pstr or 'BitTorrent protocol'
#         self.pstrlen = chr(pstrlen) or chr(len(self.pstr))
#         self.reserved = reserved or '\x00\x00\x00\x00\x00\x00\x00\x00'
#         self.info_hash = info_hash or peer.info_hash
#         self.peer_id = peer_id or MY_PEER_ID
#         self.handshake = self.pstrlen + self.pstr + self.reserved + self.info_hash + self.peer_id

# my_handshake = Handshake()
# print my_handshake.handshake

# def parse_handshake(datum):
#     if datum[1:20] == 'BitTorrent protocol':
#         received_handshake = Handshake(peer, reserved=datum[20:28], info_hash=datum[28:48], peer_id=datum[48:])
#         print 'got the handshake: ' + received_handshake.handshake
#     else:
#         pass

import message
import bt

req = message.generate_message('request', index=2, begin=0, length=2**14)
import pdb
pdb.set_trace()
示例#12
0
文件: piece_test.py 项目: phsteve/bt
 def test_piece_generation(self):
     msg = message.generate_message('piece', index=0, begin=0, block=struct.pack('>i', 2)*(2**14))
     self.assertEquals(msg.index, 0)
     self.assertEquals(msg.message_len, 9+msg.block_len)
     self.assertEquals(msg.begin, 0)
示例#13
0
文件: bt.py 项目: phsteve/bt
 def send_control_message(self, type, peer_id):
     # sends a message (either choke, unchoke, interested, or not interested) to a peer
     msg = generate_message(type, peer_id=peer_id)
     self.peer_dict[peer_id].factory.transport.write(msg.bytes)
示例#14
0
文件: bt.py 项目: phsteve/bt
 def send_control_message(self, type, peer_id):
     # sends a message (either choke, unchoke, interested, or not interested) to a peer
     msg = generate_message(type, peer_id=peer_id)
     self.peer_dict[peer_id].factory.transport.write(msg.bytes)
示例#15
0
文件: bt.py 项目: phsteve/bt
 def request_handler(self, req):
     # <len=0013><id=6><index><begin><length>
     self.received_file.seek(self.torrent.piece_length * req.index + req.begin)
     piece = generate_message('piece', index=req.index, begin=req.begin, block=self.received_file.read(req.length))
     self.peer_dict[req.peer_id].factory.transport.write(piece.bytes)