本文整理汇总了Python中lib.subtl.subtl.UdpTrackerClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Python UdpTrackerClient.connect方法的具体用法?Python UdpTrackerClient.connect怎么用?Python UdpTrackerClient.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.subtl.subtl.UdpTrackerClient
的用法示例。
在下文中一共展示了UdpTrackerClient.connect方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: announceTrackerUdp
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def announceTrackerUdp(self, tracker_address, mode="start", num_want=10):
s = time.time()
if config.disable_udp:
raise AnnounceError("Udp disabled by config")
if config.trackers_proxy != "disable":
raise AnnounceError("Udp trackers not available with proxies")
ip, port = tracker_address.split("/")[0].split(":")
tracker = UdpTrackerClient(ip, int(port))
if "ip4" in self.getOpenedServiceTypes():
tracker.peer_port = self.fileserver_port
else:
tracker.peer_port = 0
tracker.connect()
if not tracker.poll_once():
raise AnnounceError("Could not connect")
tracker.announce(info_hash=hashlib.sha1(self.site.address).hexdigest(), num_want=num_want, left=431102370)
back = tracker.poll_once()
if not back:
raise AnnounceError("No response after %.0fs" % (time.time() - s))
elif type(back) is dict and "response" in back:
peers = back["response"]["peers"]
else:
raise AnnounceError("Invalid response: %r" % back)
return peers
示例2: announce
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def announce(self, force=False):
if time.time() < self.last_announce+15 and not force: return # No reannouncing within 15 secs
self.last_announce = time.time()
errors = []
for protocol, ip, port in SiteManager.TRACKERS:
if protocol == "udp":
# self.log.debug("Announcing to %s://%s:%s..." % (protocol, ip, port))
tracker = UdpTrackerClient(ip, port)
tracker.peer_port = config.fileserver_port
try:
tracker.connect()
tracker.poll_once()
tracker.announce(info_hash=hashlib.sha1(self.address).hexdigest(), num_want=50)
back = tracker.poll_once()
peers = back["response"]["peers"]
except Exception, err:
errors.append("%s://%s:%s" % (protocol, ip, port))
continue
added = 0
for peer in peers:
if (peer["addr"], peer["port"]) in self.peer_blacklist: # Ignore blacklist (eg. myself)
continue
if self.addPeer(peer["addr"], peer["port"]): added += 1
if added:
self.worker_manager.onPeers()
self.updateWebsocket(peers_added=added)
self.settings["peers"] = len(peers)
self.saveSettings()
self.log.debug("Found %s peers, new: %s" % (len(peers), added))
else:
pass # TODO: http tracker support
示例3: announce
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def announce(self, force=False):
if time.time() < self.last_announce+60 and not force: return # No reannouncing within 60 secs
self.last_announce = time.time()
errors = []
address_hash = hashlib.sha1(self.address).hexdigest()
my_peer_id = sys.modules["main"].file_server.peer_id
# Later, if we have peer exchange
"""if sys.modules["main"].file_server.port_opened:
fileserver_port = config.fileserver_port
else: # Port not opened, report port 0
fileserver_port = 0"""
fileserver_port = config.fileserver_port
s = time.time()
announced = 0
for protocol, ip, port in SiteManager.TRACKERS:
if protocol == "udp": # Udp tracker
if config.disable_udp: continue # No udp supported
tracker = UdpTrackerClient(ip, port)
tracker.peer_port = fileserver_port
try:
tracker.connect()
tracker.poll_once()
tracker.announce(info_hash=address_hash, num_want=50)
back = tracker.poll_once()
peers = back["response"]["peers"]
except Exception, err:
errors.append("%s://%s:%s" % (protocol, ip, port))
continue
else: # Http tracker
params = {
'info_hash': binascii.a2b_hex(address_hash),
'peer_id': my_peer_id, 'port': fileserver_port,
'uploaded': 0, 'downloaded': 0, 'left': 0, 'compact': 1, 'numwant': 30,
'event': 'started'
}
try:
url = "http://"+ip+"?"+urllib.urlencode(params)
# Load url
opener = urllib2.build_opener()
response = opener.open(url, timeout=10).read()
# Decode peers
peer_data = bencode.decode(response)["peers"]
peer_count = len(peer_data) / 6
peers = []
for peer_offset in xrange(peer_count):
off = 6 * peer_offset
peer = peer_data[off:off + 6]
addr, port = struct.unpack('!LH', peer)
peers.append({"addr": socket.inet_ntoa(struct.pack('!L', addr)), "port": port})
except Exception, err:
self.log.debug("Http tracker %s error: %s" % (url, err))
errors.append("%s://%s" % (protocol, ip))
continue
示例4: announceTracker
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def announceTracker(self, protocol, ip, port, fileserver_port, address_hash, my_peer_id):
s = time.time()
if protocol == "udp": # Udp tracker
if config.disable_udp: return False # No udp supported
tracker = UdpTrackerClient(ip, port)
tracker.peer_port = fileserver_port
try:
tracker.connect()
tracker.poll_once()
tracker.announce(info_hash=address_hash, num_want=50)
back = tracker.poll_once()
peers = back["response"]["peers"]
except Exception, err:
return False
示例5: announceTracker
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def announceTracker(self, tracker_protocol, tracker_address, fileserver_port=0, add_types=[], my_peer_id="", mode="start"):
s = time.time()
if "ip4" not in add_types:
fileserver_port = 0
if tracker_protocol == "udp": # Udp tracker
if config.disable_udp:
return False # No udp supported
ip, port = tracker_address.split(":")
tracker = UdpTrackerClient(ip, int(port))
tracker.peer_port = fileserver_port
try:
tracker.connect()
tracker.poll_once()
tracker.announce(info_hash=hashlib.sha1(self.address).hexdigest(), num_want=50)
back = tracker.poll_once()
peers = back["response"]["peers"]
except Exception, err:
return False
示例6: testTrackers
# 需要导入模块: from lib.subtl.subtl import UdpTrackerClient [as 别名]
# 或者: from lib.subtl.subtl.UdpTrackerClient import connect [as 别名]
def testTrackers(self):
raise unittest.SkipTest("Notyet")
from Site import SiteManager
from lib.subtl.subtl import UdpTrackerClient
import hashlib
ok = 0
for protocol, ip, port in SiteManager.TRACKERS:
address = "test"
if protocol == "udp":
tracker = UdpTrackerClient(ip, port)
peers = None
try:
tracker.connect()
tracker.poll_once()
tracker.announce(info_hash=hashlib.sha1(address).hexdigest(), num_want=5)
back = tracker.poll_once()
peers = back["response"]["peers"]
except Exception, err:
peers = None
print "Tracker error: %s://%s:%s %s" % (protocol, ip, port, err)
if peers != None:
ok += 1