Skip to content

Commit

Permalink
improve logging & update requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
gallexis committed Jul 19, 2020
1 parent 909311b commit 4db6e9d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
15 changes: 9 additions & 6 deletions main.py
Expand Up @@ -14,6 +14,7 @@

class Run(object):
percentage_completed = -1
last_log_line = ""

def __init__(self):
self.torrent = torrent.Torrent().load_from_path("torrent.torrent")
Expand Down Expand Up @@ -56,7 +57,7 @@ def start(self):
piece_data = message.Request(piece_index, block_offset, block_length).to_bytes()
peer.send_to_peer(piece_data)

self.display_progression()
self.display_progression()

time.sleep(0.1)

Expand All @@ -79,12 +80,14 @@ def display_progression(self):
number_of_peers = self.peers_manager.unchoked_peers_count()
percentage_completed = float((float(new_progression) / self.torrent.total_length) * 100)

print("Number of peers: {} - Completed : {}% | Got {}/{} pieces".
format(number_of_peers,
round(percentage_completed, 2),
self.pieces_manager.complete_pieces,
self.pieces_manager.number_of_pieces))
current_log_line = "Connected peers: {} - {}% completed | {}/{} pieces".format(number_of_peers,
round(percentage_completed, 2),
self.pieces_manager.complete_pieces,
self.pieces_manager.number_of_pieces)
if current_log_line != self.last_log_line:
print(current_log_line)

self.last_log_line = current_log_line
self.percentage_completed = new_progression

def _exit_threads(self):
Expand Down
6 changes: 4 additions & 2 deletions message.py
Expand Up @@ -14,6 +14,7 @@
class WrongMessageException(Exception):
pass


class MessageDispatcher:

def __init__(self, payload):
Expand All @@ -22,8 +23,8 @@ def __init__(self, payload):
def dispatch(self):
try:
payload_length, message_id, = unpack(">IB", self.payload[:5])
except:
logging.exception("Error when unpacking message")
except Exception as e:
logging.warning("Error when unpacking message : %s" % e.__str__())
return None

map_id_to_message = {
Expand Down Expand Up @@ -189,6 +190,7 @@ def _parse_sock_addr(self, raw_bytes):
Bittorrent messages
"""


class Handshake(Message):
"""
Handshake = <pstrlen><pstr><reserved><info_hash><peer_id>
Expand Down
10 changes: 5 additions & 5 deletions peer.py
Expand Up @@ -188,8 +188,8 @@ def get_messages(self):
self.read_buffer = self.read_buffer[total_length:]

try:
m = message.MessageDispatcher(payload).dispatch()
if m:
yield m
except message.WrongMessageException:
logging.exception("")
received_message = message.MessageDispatcher(payload).dispatch()
if received_message:
yield received_message
except message.WrongMessageException as e:
logging.exception(e.__str__())
8 changes: 4 additions & 4 deletions requirements.txt
@@ -1,6 +1,6 @@
bcoding==1.5
bitstring == 3.1.5
PyPubSub == 3.3.0
bitstring == 3.1.7
PyPubSub == 4.0.3
requests >= 2.24.0
pubsub == 0.1.1
ipaddress == 1.0.22
pubsub == 0.1.2
ipaddress == 1.0.23
4 changes: 2 additions & 2 deletions tracker.py
Expand Up @@ -93,8 +93,8 @@ def http_scraper(self, torrent, tracker):
s = SockAddr(p['ip'], p['port'])
self.dict_sock_addr[s.__hash__()] = s

except Exception:
logging.exception("HTTP scraping failed")
except Exception as e:
logging.exception("HTTP scraping failed: %s" % e.__str__())

def udp_scrapper(self, announce):
torrent = self.torrent
Expand Down

0 comments on commit 4db6e9d

Please sign in to comment.