您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

torrenttracker.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #! /usr/bin/env python
  2. import requests
  3. import time
  4. import hashlib
  5. import bencode
  6. class TorrentTracker:
  7. torrent = None
  8. session = None
  9. peer_id = ""
  10. port = 0
  11. user_agent = ""
  12. interval = 300
  13. def __init__(self, torrent, port=6882, user_agent="rtorrent/0.8.42/0.12.42"):
  14. self.torrent = torrent
  15. self.session = requests.session()
  16. self.peer_id = hashlib.sha1(str(time.time())).digest()
  17. self.port = port
  18. self.user_agent = user_agent
  19. def getInterval(self):
  20. return self.interval
  21. def request(self, uploaded, downloaded, left, event=""):
  22. params = {"info_hash": self.torrent.getHash().digest(),
  23. "peer_id": self.peer_id,
  24. "port": self.port,
  25. "uploaded": uploaded,
  26. "downloaded": downloaded,
  27. "left": left,
  28. "event": event}
  29. headers = {"User-Agent": self.user_agent}
  30. r = self.session.get(self.torrent.getAnnounce(), headers=headers, params=params)
  31. data = bencode.bdecode(r.content)
  32. if 'interval' in data:
  33. self.interval = int(data['interval'])
  34. return data;
  35. def start(self):
  36. return self.request(0, 0, 0, "started")
  37. def stop(self, uploaded, downloaded, left):
  38. return self.request(0, 0, 0, "stopped")
  39. def complete(self, uploaded):
  40. return self.request(0, 0, 0, "completed")
  41. def interval(self, uploaded, downloaded, left):
  42. return self.request(uploaded, downloaded, left)