本文整理汇总了Python中Messages.bytes_to_number方法的典型用法代码示例。如果您正苦于以下问题:Python Messages.bytes_to_number方法的具体用法?Python Messages.bytes_to_number怎么用?Python Messages.bytes_to_number使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Messages
的用法示例。
在下文中一共展示了Messages.bytes_to_number方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleData
# 需要导入模块: import Messages [as 别名]
# 或者: from Messages import bytes_to_number [as 别名]
def handleData(self, data = ""):
self.buffer += data
message = ""
if(self.buffer[1:20].lower() == "bittorrent protocol"):
message += self.buffer[:68]
self.buffer = self.buffer[68:]
return message
message_length = Messages.bytes_to_number(self.buffer[:4]) + 4
if(len(self.buffer) >= message_length):
message = self.buffer[:message_length]
self.buffer = self.buffer[message_length:]
return message
return None
示例2: process_message
# 需要导入模块: import Messages [as 别名]
# 或者: from Messages import bytes_to_number [as 别名]
def process_message(self, message_obj):
fmt = 'Peer : {:16s} || {:35s}'
if isinstance(message_obj, Messages.Choke):
Logger.info(fmt.format(str(self.ip) , "Choke"))
self.choked = True
elif isinstance(message_obj, Messages.Unchoke):
Logger.info(fmt.format(str(self.ip) , "UnChoke"))
self.choked = False
elif isinstance(message_obj, Messages.Interested):
Logger.info(fmt.format(str(self.ip) , "Interested"))
self.peer_interested = True
elif isinstance(message_obj, Messages.NotInterested):
Logger.info(fmt.format(str(self.ip) , "Not Interested"))
self.peer_interested = False
elif isinstance(message_obj, Messages.Have):
piece_index = Messages.bytes_to_number(message_obj.index)
self.torrent.requester.havePiece(self, piece_index)
Logger.debug(fmt.format(str(self.ip) , "Has Piece Index :" + str(piece_index)))
ret = self.torrent.fileManager.checkBounds(piece_index,0)
if ret != 2:
self.interested = True;
if self.peer_choked == True:
self.transport.write(str(Messages.Unchoke()))
self.peer_choked = False
self.peer_has_pieces[piece_index] = 1
elif isinstance(message_obj, Messages.Bitfield):
Logger.info(str("Peer :") + str(self.ip) + " || " + "Bitfield")
self.torrent.requester.haveBitfield(self,message_obj.bitfield)
bitarray = BitArray(bytes=message_obj.bitfield)
self.peer_has_pieces = bitarray[:len(self.peer_has_pieces)]
flag = False
for i in range(self.torrent.fileManager.start_piece_id, len(message_obj.bitfield)):
ret = self.torrent.fileManager.checkBounds(i,0)
if ret != 2:
if message_obj.bitfield[i] == True:
flag = True
break
else:
break
if flag == True:
self.interested = True
self.transport.write(str(Messages.Unchoke()))
self.peer_choked = False
elif isinstance(message_obj, Messages.Request):
Logger.info(fmt.format(str(self.ip) , "Request"))
if self.peer_choked == False:
answer_request(message_obj.index, message_obj.begin, message_obj.length)
elif isinstance(message_obj, Messages.Piece):
# Unpdate Number of Pending Requests
self.pending_requests -= 1
# Get the (Piece Index, Block Offset) Pair
piece_index = Messages.bytes_to_number(message_obj.index)
block_byte_offset = Messages.bytes_to_number(message_obj.begin)
block_index = block_byte_offset/BLOCK_SIZE
# Logger.info(fmt.format(str(self.ip) , str(piece_index) + "," + str(block_index) + " :: Recevied Data Piece!!! !:D :D :D"))
self.torrent.requester.updateTotalDataReceived(len(message_obj.block))
if((piece_index,block_index) in self.set_of_blocks_requested):
self.set_of_blocks_requested.remove((piece_index,block_index))
self.set_of_blocks_received.add((piece_index,block_index))
message_obj.index = Messages.bytes_to_number(message_obj.index)
message_obj.begin = Messages.bytes_to_number(message_obj.begin)
self.writeData(message_obj.index, message_obj.begin, message_obj.block)
# Logger.info("Received || Total Current Requests :" + str(self.torrent.requester.total_requests))
# Logger.info("Received || Wasted Requests:" + str(self.torrent.requester.total_requests_wasted))
# Logger.info("Received || Cancelled Requests:" + str(self.torrent.requester.total_requests_cancelled))
# Logger.info("Received || Requests Used:" + str(self.torrent.requester.total_requests_used))
# Logger.info("Received || Total Requests Sent:" + str(self.torrent.requester.total_requests_sent))
elif isinstance(message_obj, Messages.Cancel):
Logger.info(fmt.format(str(self.ip) , "Cancelled Request :\\"))
elif isinstance(message_obj, Messages.Port):
Logger.info(fmt.format(str(self.ip) , "Natalie Portman ? :D"))
示例3: process_message
# 需要导入模块: import Messages [as 别名]
# 或者: from Messages import bytes_to_number [as 别名]
def process_message(self, message_obj):
fmt = 'Peer : {:16s} || {:35s}'
if isinstance(message_obj, Messages.Choke):
Logger.info(fmt.format(str(self.ip) , "Choke"))
self.choked = True
elif isinstance(message_obj, Messages.Unchoke):
Logger.info(fmt.format(str(self.ip) , "UnChoke"))
self.choked = False
elif isinstance(message_obj, Messages.Interested):
Logger.info(fmt.format(str(self.ip) , "Interested"))
self.peer_interested = True
elif isinstance(message_obj, Messages.NotInterested): #Should be NotInterested
Logger.info(fmt.format(str(self.ip) , "Not Interested"))
self.peer_interested = False
elif isinstance(message_obj, Messages.Have):
piece_index = Messages.bytes_to_number(message_obj.index)
self.torrent.requester.havePiece(self,piece_index)
Logger.info(fmt.format(str(self.ip) , "Has Piece Index :" + str(piece_index)))
ret = self.torrent.file_manager.checkBounds(piece_index,0)
if ret != 2:
self.interested = True;
self.transport.write(str(Messages.Unchoke()))
self.peer_choked = False
self.peer_has_pieces[piece_index] = 1
elif isinstance(message_obj, Messages.Bitfield):
Logger.info(str("Peer :") + str(self.ip) + " || " + "Bitfield")
self.torrent.requester.haveBitfield(self,message_obj.bitfield)
bitarray = BitArray(bytes=message_obj.bitfield)
self.peer_has_pieces = bitarray[:len(self.peer_has_pieces)]
flag = False
for i in range(self.torrent.file_manager.start_piece_id,len(message_obj.bitfield)):
ret = self.torrent.file_manager.checkBounds(i,0)
if ret != 2:
if message_obj.bitfield[i] == True:
flag = True
break
else:
break
if flag == True:
self.interested = True
self.transport.write(str(Messages.Unchoke()))
self.peer_choked = False
# assert self.torrent.file_manager.no_of_pieces == len(message_obj.bitfield), str("Peer Has Pieces is not of same length as Message_obj.bitfield" + str(self.torrent.file_manager.no_of_pieces) + "|.|.|.|.|" +str(len(message_obj.bitfield)))
elif isinstance(message_obj, Messages.Request):
Logger.info(fmt.format(str(self.ip) , "Request"))
if self.peer_choked == False:
answer_request(message_obj.index,message_obj.begin,message_obj.length)
#TODO: implement sending pieces/serving torrents
elif isinstance(message_obj, Messages.Piece):
self.pending_requests -= 1
piece_index = Messages.bytes_to_number(message_obj.index)
block_byte_offset = Messages.bytes_to_number(message_obj.begin)
block_index = block_byte_offset/BLOCK_SIZE
Logger.info(fmt.format(str(self.ip) , str(piece_index) + "," + str(block_index) + " :: Recevied Data Piece!!! !:D :D :D"))
self.torrent.requester.updateTotalDataReceived(len(message_obj.block))
if((piece_index,block_index) in self.set_of_blocks_requested):
self.set_of_blocks_requested.remove((piece_index,block_index))
self.set_of_blocks_received.add((piece_index,block_index))
#Change the following line as per the function name used in FileManager.
message_obj.index = Messages.bytes_to_number(message_obj.index)
message_obj.begin = Messages.bytes_to_number(message_obj.begin)
self.writeData(message_obj.index, message_obj.begin, message_obj.block)
Logger.info("Received || Total Current Requests :" + str(self.torrent.requester.total_requests))
Logger.info("Received || Wasted Requests:" + str(self.torrent.requester.total_requests_wasted))
Logger.info("Received || Cancelled Requests:" + str(self.torrent.requester.total_requests_cancelled))
Logger.info("Received || Requests Used:" + str(self.torrent.requester.total_requests_used))
Logger.info("Received || Total Requests Sent:" + str(self.torrent.requester.total_requests_sent))
# assert self.torrent.requester.total_requests_sent == self.torrent.requester.total_requests_wasted + self.torrent.requester.total_requests_cancelled + self.torrent.requester.total_requests_used, "Chutiyapa in count of requests wasted and Cancelled"
elif isinstance(message_obj, Messages.Cancel):
Logger.info(fmt.format(str(self.ip) , "Cancelled Request :\\"))
elif isinstance(message_obj, Messages.Port):
Logger.info(fmt.format(str(self.ip) , "Natalie Portman ? :D"))