本文整理汇总了Python中BaseLib.Core.TorrentDef.TorrentDef.finalize方法的典型用法代码示例。如果您正苦于以下问题:Python TorrentDef.finalize方法的具体用法?Python TorrentDef.finalize怎么用?Python TorrentDef.finalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BaseLib.Core.TorrentDef.TorrentDef
的用法示例。
在下文中一共展示了TorrentDef.finalize方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testECreateRichTorrents
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
def testECreateRichTorrents(self):
_log.info("Create rich metadata torrents ------------")
if not TestMetaGenerator.run_test:
_log.debug("Test avoided.")
return
data = None
self.assertTrue(TestMetaGenerator.data != None, "Test E depends on data gathered in test D, set testD attribute to True")
if TestMetaGenerator.data != None:
data = TestMetaGenerator.data
else:
return
rmg = RichMetadataGenerator.getInstance()
torrents = []
for k, v in data.items():
if v.content_base != None:
# Choose only a small subset of content to download
if v.content_base == "http://www.rtvslo.si/podcasts/gori_doli_naokoli.xml":
mm_file = tempfile.NamedTemporaryFile()
try:
urllib.urlretrieve(v.id, mm_file.name)
torrent_def = TorrentDef()
torrent_def.add_content(mm_file.name)
torrent_def.set_tracker("http://wanabe.a.tracker.com:8080")
torrent_def.set_metadata(k)
torrent_def.finalize()
torrent_def.save(mm_file.name + ".torrent")
torrents.append(mm_file.name + ".torrent")
except Exception, e:
_log.error("Failed to generate torrent file, reason: " + str(e))
示例2: create_torrent
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
def create_torrent(file, port, try_vod = True):
'''
Creates a torrent for the given file. Returns a string representing the
path to the torrent file.
If try_vod is True try to add the playtime to the torrent making it "streamable".
'''
# generate torrent
tdef = TorrentDef()
url = "http://localhost:%s/announce/" % port
tdef.set_tracker(url)
#tdef.set_create_merkle_torrent(True)
#tdef.set_piece_length(self._pieceSize)
if try_vod:
torrent_name = FileUtils.get_relative_filename(file) + constants.TORRENT_VOD_EXT
# set playtime
playtime = videostats_via_ffmpeg(file)['playtime']
print "playtime", playtime
else:
torrent_name = FileUtils.get_relative_filename(file) + constants.TORRENT_DOWNLOAD_EXT
playtime = None
print "Create a non-streamable torrent"
tdef.add_content(file, playtime=playtime)
tdef.finalize()
torrent_dir = os.getcwd()
torrent = os.path.join(torrent_dir, torrent_name)
tdef.save(torrent)
print "created torrent file: %s" % torrent
print "Tracker uses the announce URL: %s" % tdef.get_tracker()
return tdef
示例3: TestMetadata
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
class TestMetadata(TestAsServer, MagnetHelpers):
"""
Once we are downloading a torrent, our client should respond to
the ut_metadata extention message. This allows other clients to
obtain the info part of the metadata from us.
"""
def setUp(self):
""" override TestAsServer """
TestAsServer.setUp(self)
print >>sys.stderr,time.asctime(),'-', "test: Giving MyLaunchMany time to startup"
time.sleep(5)
print >>sys.stderr,time.asctime(),'-', "test: MyLaunchMany should have started up"
# the metadata that we want to transfer
self.tdef = TorrentDef()
self.tdef.add_content(os.path.join(os.getcwd(), "API", "file.wmv"))
self.tdef.set_tracker(self.session.get_internal_tracker_url())
# we use a small piece length to obtain multiple pieces
self.tdef.set_piece_length(1)
self.tdef.finalize()
# self.tdef.save(os.path.join(self.session.get_state_dir(), "gen.torrent"))
MagnetHelpers.__init__(self, self.tdef)
def setup_seeder(self):
self.seeder_setup_complete = False
self.seeder_teardown_complete = False
self.seeder_teardown = False
self.dscfg = DownloadStartupConfig()
self.dscfg.set_dest_dir(os.getcwd())
self.download = self.session.start_download(self.tdef, self.dscfg)
self.download.set_state_callback(self.seeder_state_callback)
counter = 0
while not self.seeder_setup_complete:
counter += 1
time.sleep(1)
assert counter < 30, "timeout"
print >> sys.stderr, time.asctime(),'-', "test: setup_seeder() complete"
def teardown_seeder(self):
self.seeder_teardown_complete = False
self.session.remove_download(self.download)
counter = 0
while not self.seeder_setup_complete:
counter += 1
time.sleep(1)
assert counter < 30, "timeout"
print >> sys.stderr, time.asctime(),'-', "test: teardown_seeder() complete"
def seeder_state_callback(self,ds):
assert not self.seeder_teardown_complete
self.seeder_setup_complete = (ds.get_status() == DLSTATUS_DOWNLOADING)
d = ds.get_download()
print >> sys.stderr, time.asctime(),'-', "test: seeder:", `d.get_def().get_name()`, dlstatus_strings[ds.get_status()], ds.get_progress()
if self.seeder_teardown:
self.seeder_teardown_complete = True
else:
return (1.0, False)
def test_all(self):
self.setup_seeder()
try:
self.subtest_good_flood()
finally:
self.teardown_seeder()
self.setup_seeder()
try:
self.subtest_good_request()
self.subtest_bad_request()
finally:
self.teardown_seeder()
def subtest_good_request(self):
conn = BTConnection("localhost", self.hisport, user_infohash=self.tdef.get_infohash())
conn.send(self.create_good_extend_handshake())
conn.read_handshake_medium_rare()
metadata_id = self.read_extend_handshake(conn)
# request metadata block 0, 2, 3, and the last
conn.send(self.create_good_extend_metadata_request(metadata_id, 0))
conn.send(self.create_good_extend_metadata_request(metadata_id, 2))
conn.send(self.create_good_extend_metadata_request(metadata_id, 3))
conn.send(self.create_good_extend_metadata_request(metadata_id, len(self.metadata_list) - 1))
self.read_extend_metadata_reply(conn, 0)
self.read_extend_metadata_reply(conn, 2)
self.read_extend_metadata_reply(conn, 3)
self.read_extend_metadata_reply(conn, len(self.metadata_list) - 1)
def subtest_good_flood(self):
conn = BTConnection("localhost", self.hisport, user_infohash=self.tdef.get_infohash())
conn.send(self.create_good_extend_handshake())
conn.read_handshake_medium_rare()
metadata_id = self.read_extend_handshake(conn)
#.........这里部分代码省略.........
示例4: TestMagnetMiniBitTorrent
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
class TestMagnetMiniBitTorrent(TestAsServer, MagnetHelpers):
"""
A MiniBitTorrent instance is used to connect to BitTorrent clients
and download the info part from the metadata.
"""
def setUp(self):
""" override TestAsServer """
# listener for incoming connections from MiniBitTorrent
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind(("localhost", LISTEN_PORT))
self.server.listen(1)
# the metadata that we want to transfer
self.tdef = TorrentDef()
self.tdef.add_content(os.path.join(os.getcwd(), "API", "file.wmv"))
self.tdef.set_tracker("http://fake.net/announce")
# we use a small piece length to obtain multiple pieces
self.tdef.set_piece_length(1)
self.tdef.finalize()
MagnetHelpers.__init__(self, self.tdef)
# startup the client
TestAsServer.setUp(self)
print >>sys.stderr,time.asctime(),'-', "test: Giving MyLaunchMany time to startup"
time.sleep(5)
print >>sys.stderr,time.asctime(),'-', "test: MyLaunchMany should have started up"
def create_good_url(self, infohash=None, title=None, tracker=None):
url = "magnet:?xt=urn:btih:"
if infohash:
assert isinstance(infohash, str)
url += hexlify(infohash)
else:
url += hexlify(self.tdef.get_infohash())
if title:
assert isinstance(title, str)
url += "&dn=" + title
if tracker:
assert isinstance(tracker, str)
url += "&tr=" + tracker
return url
def test_good_transfer(self):
def torrentdef_retrieved(tdef):
tags["retrieved"] = True
tags["metainfo"] = tdef.get_metainfo()
tags = {"retrieved":False}
assert TorrentDef.retrieve_from_magnet(self.create_good_url(), torrentdef_retrieved)
# supply fake addresses (regular dht obviously wont work here)
for magnetlink in MagnetHandler.get_instance().get_magnets():
magnetlink._swarm.add_potential_peers([("localhost", LISTEN_PORT)])
# accept incoming connection
self.server.settimeout(10.0)
sock, address = self.server.accept()
assert sock, "No incoming connection"
# handshakes
conn = BTConnection(address[0], address[1], opensock=sock, user_infohash=self.tdef.get_infohash())
conn.send(self.create_good_extend_handshake())
conn.read_handshake_medium_rare()
metadata_id = self.read_extend_handshake(conn)
# serve pieces
for counter in xrange(len(self.metadata_list)):
piece = self.read_extend_metadata_request(conn)
assert 0 <= piece < len(self.metadata_list)
conn.send(self.create_good_extend_metadata_reply(metadata_id, piece))
# no more metadata request may be send and the connection must
# be closed
self.read_extend_metadata_close(conn)
time.sleep(5)
assert tags["retrieved"]
assert tags["metainfo"]["info"] == self.tdef.get_metainfo()["info"]
示例5: TestVideoOnDemand
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
class TestVideoOnDemand(TestAsServer):
"""
Testing Merkle hashpiece messages for both:
* Merkle BEP style
* old Tribler <= 4.5.2 that did not use the Extention protocol (BEP 10).
See BitTornado/BT1/Connecter.py
"""
def setUp(self):
""" override TestAsServer """
TestAsServer.setUp(self)
self.vodstarted = False
def setUpPreSession(self):
""" override TestAsServer """
TestAsServer.setUpPreSession(self)
self.config.set_overlay(False)
self.config.set_megacache(False)
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
def tearDown(self):
print >> sys.stderr, time.asctime(), "-", "Test: Sleep before tear down"
time.sleep(10)
TestAsServer.tearDown(self)
def create_torrent(self):
[srchandle, self.sourcefn] = mkstemp()
self.content = Rand.rand_bytes(self.contentlen)
os.write(srchandle, self.content)
os.close(srchandle)
self.tdef = TorrentDef()
self.tdef.add_content(self.sourcefn)
self.tdef.set_piece_length(self.piecelen)
self.tdef.set_tracker("http://127.0.0.1:12/announce")
self.tdef.finalize()
self.torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent")
self.tdef.save(self.torrentfn)
dscfg = DownloadStartupConfig()
destdir = os.path.dirname(self.sourcefn)
dscfg.set_dest_dir(destdir)
dscfg.set_video_event_callback(self.sesscb_vod_event_callback)
self.session.set_download_states_callback(self.states_callback)
self.session.start_download(self.tdef, dscfg)
def states_callback(self, dslist):
ds = dslist[0]
d = ds.get_download()
# print >>sys.stderr,time.asctime(),'-', `d.get_def().get_name()`,dlstatus_strings[ds.get_status()],ds.get_progress(),"%",ds.get_error(),"up",ds.get_current_speed(UPLOAD),"down",ds.get_current_speed(DOWNLOAD)
print >> sys.stderr, time.asctime(), "-", "%s %s %5.2f%% %s up %8.2fKB/s down %8.2fKB/s" % (
d.get_def().get_name(),
dlstatus_strings[ds.get_status()],
ds.get_progress() * 100,
ds.get_error(),
ds.get_current_speed(UPLOAD),
ds.get_current_speed(DOWNLOAD),
)
return (1.0, False)
def sesscb_vod_event_callback(self, d, event, params):
if self.vodstarted:
return
self.vodstarted = True
print >> sys.stderr, time.asctime(), "-", "Test: vod_event_callback", event, params
if event == VODEVENT_START:
stream = params["stream"]
# Read last piece
lastpieceoff = ((self.contentlen - 1) / self.piecelen) * self.piecelen
lastpiecesize = self.contentlen - lastpieceoff
print >> sys.stderr, time.asctime(), "-", "Test: stream: lastpieceoff", lastpieceoff, lastpiecesize
self.stream_read(stream, lastpieceoff, lastpiecesize, self.piecelen)
# Read second,3rd,4th byte, only
secoff = 1
secsize = 3
blocksize = 3
self.stream_read(stream, secoff, secsize, blocksize)
# Read last byte
lastoff = self.contentlen - 1
lastsize = 1
self.stream_read(stream, lastoff, lastsize, self.piecelen)
print >> sys.stderr, time.asctime(), "-", "Test: stream: Passed?"
def stream_read(self, stream, off, size, blocksize):
stream.seek(off)
#.........这里部分代码省略.........
示例6: TestQueryReplyActive
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
class TestQueryReplyActive(TestAsServer):
"""
Testing QUERY_REPLY message of Query extension V1
This test checks how the Tribler code responds to good and bad
QUERY_REPLY messages. I.e. the Tribler client initiates
the dialback by connecting to us and sending a QUERY and we
reply with good and bad messages.
This test allows authoritative answers from superpeers.
WARNING: Each of the test_ methods should be tested by running the TestCase
in a separate Python interpreter to prevent problems with our singleton
classes, e.g. SuperPeerDB, etc.
"""
def setUpPreSession(self):
""" override TestAsServer """
print >> sys.stderr,"test: Pre Tribler Init"
TestAsServer.setUpPreSession(self)
print >> sys.stderr,"test: Pre Tribler Init: config_path",self.config_path
# Enable remote querying
self.config.set_remote_query(True)
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
self.hispermid = str(self.his_keypair.pub().get_der())
self.my_permid = str(self.my_keypair.pub().get_der())
self.content_name = 'Hallo S22E44'
self.tdef = TorrentDef()
self.tdef.set_tracker('http://localhost:0/announce')
self.tdef.set_piece_length(2 ** 15)
self.tdef.create_live(self.content_name,2 ** 16)
self.tdef.finalize()
def pretest_simple(self):
self.pretest_q('SIMPLE hallo')
def pretest_simpleplustorrents(self):
self.pretest_q('SIMPLE+METADATA hallo')
def pretest_q(self,query):
# 1. First connect to Tribler
self.openconn = OLConnection(self.my_keypair,'localhost',self.hisport)
sleep(3)
# 2. Make Tribler send query
self.query = query
self.session.query_connected_peers(query,self.query_usercallback,max_peers_to_query=10)
def query_usercallback(self,permid,query,hits):
print >>sys.stderr,"test: query_usercallback:",`permid`,`query`,`hits`
self.assert_(query == self.query)
self.assert_(permid == self.my_permid)
self.check_good_qreply(hits)
# TODO: if SIMPLE+METADATA: check torrent now in db.
#
# Good SIMPLE QUERY, builds on TestQueryReply code
#
def singtest_good_simple_reply(self):
self.pretest_simple()
self._test_qreply(self.create_good_simple_reply,True)
#
# Good SIMPLE+METADATA QUERY, builds on TestQueryReply code
#
def singtest_good_simpleplustorrents_reply(self):
self.pretest_simpleplustorrents()
self._test_qreply(self.create_good_simpleplustorrents_reply,True)
#
# Bad QUERY, builds on TestQueryReply code
#
def singtest_bad_not_bdecodable(self):
self.pretest_simple()
self._test_qreply(self.create_not_bdecodable,False)
#
# Bad SIMPLE+METADATA QUERY, builds on TestQueryReply code
#
def singtest_bad_not_bdecodable_torrentfile(self):
self.pretest_simpleplustorrents()
self._test_qreply(self.create_not_bdecodable_torrentfile,False)
### TODO: send different valid answers so consensus not reached
#
# Main test code
#
#.........这里部分代码省略.........
示例7: setUpPostSession
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
# Let Tribler start downloading an non-functioning torrent, so
# we can talk to a normal download engine.
self.tdef = TorrentDef()
self.sourcefn = os.path.join(os.getcwd(),"API","file2.wmv")
self.tdef.add_content(self.sourcefn)
self.tdef.set_create_merkle_torrent(True)
self.tdef.set_tracker("http://127.0.0.1:12/announce")
self.tdef.finalize()
self.torrentfn = os.path.join(self.session.get_state_dir(),"gen.torrent")
self.tdef.save(self.torrentfn)
dscfg = self.setUpDownloadConfig()
self.session.start_download(self.tdef,dscfg)
self.infohash = self.tdef.get_infohash()
self.mylistenport = 4810
self.numpieces = (self.tdef.get_length()+self.tdef.get_piece_length()-1) / self.tdef.get_piece_length()
b = Bitfield(self.numpieces)
for i in range(self.numpieces):
b[i] = True
self.assert_(b.complete())
self.seederbitfieldstr = b.tostring()
#piece_hashes = ['\x01\x02\x03\x04\x05\x06\x07\x08\x07\x06\x05\x04\x03\x02\x01\x00\x01\x02\x03\x04' ] * npieces
# Construct Merkle tree
tdef2 = TorrentDef()
tdef2.add_content(self.sourcefn)
tdef2.set_create_merkle_torrent(False)
tdef2.set_tracker("http://127.0.0.1:12/announce")
tdef2.set_piece_length(self.tdef.get_piece_length())
tdef2.finalize()
metainfo = tdef2.get_metainfo()
piecesstr = metainfo['info']['pieces']
print >>sys.stderr,"test: pieces has len",len(piecesstr)
piece_hashes = []
for i in range(0,len(piecesstr),20):
hash = piecesstr[i:i+20]
print >>sys.stderr,"test: piece",i/20,"hash",`hash`
piece_hashes.append(hash)
print >>sys.stderr,"test: Putting",len(piece_hashes),"into MerkleTree, size",self.tdef.get_piece_length(),tdef2.get_piece_length()
self.tree = MerkleTree(self.tdef.get_piece_length(),self.tdef.get_length(),None,piece_hashes)
f = open(self.sourcefn,"rb")
piece1 = f.read(2 ** 18)
piece2 = f.read(2 ** 18)
print >>sys.stderr,"read piece1",len(piece1)
print >>sys.stderr,"read piece2",len(piece2)
f.close()
hash1 = sha(piece1).digest()
hash2 = sha(piece2).digest()
print >>sys.stderr,"hash piece1",`hash1`
print >>sys.stderr,"hash piece2",`hash2`
f2 = open("piece1.bin","wb")
f2.write(piece2)
f2.close()
示例8: TestMerkleMessage
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import finalize [as 别名]
class TestMerkleMessage(TestAsServer):
"""
Testing Merkle hashpiece messages for both:
* Merkle BEP style
* old Tribler <= 4.5.2 that did not use the Extention protocol (BEP 10).
See BitTornado/BT1/Connecter.py
"""
def setUp(self):
""" override TestAsServer """
TestAsServer.setUp(self)
print >>sys.stderr,"test: Giving Session time to startup"
time.sleep(5)
print >>sys.stderr,"test: Session should have started up"
def setUpPreSession(self):
""" override TestAsServer """
TestAsServer.setUpPreSession(self)
self.config.set_overlay(False)
self.config.set_megacache(False)
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
# Let Tribler start downloading an non-functioning torrent, so
# we can talk to a normal download engine.
self.tdef = TorrentDef()
self.sourcefn = os.path.join(os.getcwd(),"API","file2.wmv")
self.tdef.add_content(self.sourcefn)
self.tdef.set_create_merkle_torrent(True)
self.tdef.set_tracker("http://127.0.0.1:12/announce")
self.tdef.finalize()
self.torrentfn = os.path.join(self.session.get_state_dir(),"gen.torrent")
self.tdef.save(self.torrentfn)
dscfg = self.setUpDownloadConfig()
self.session.start_download(self.tdef,dscfg)
self.infohash = self.tdef.get_infohash()
self.mylistenport = 4810
self.numpieces = (self.tdef.get_length()+self.tdef.get_piece_length()-1) / self.tdef.get_piece_length()
b = Bitfield(self.numpieces)
for i in range(self.numpieces):
b[i] = True
self.assert_(b.complete())
self.seederbitfieldstr = b.tostring()
#piece_hashes = ['\x01\x02\x03\x04\x05\x06\x07\x08\x07\x06\x05\x04\x03\x02\x01\x00\x01\x02\x03\x04' ] * npieces
# Construct Merkle tree
tdef2 = TorrentDef()
tdef2.add_content(self.sourcefn)
tdef2.set_create_merkle_torrent(False)
tdef2.set_tracker("http://127.0.0.1:12/announce")
tdef2.set_piece_length(self.tdef.get_piece_length())
tdef2.finalize()
metainfo = tdef2.get_metainfo()
piecesstr = metainfo['info']['pieces']
print >>sys.stderr,"test: pieces has len",len(piecesstr)
piece_hashes = []
for i in range(0,len(piecesstr),20):
hash = piecesstr[i:i+20]
print >>sys.stderr,"test: piece",i/20,"hash",`hash`
piece_hashes.append(hash)
print >>sys.stderr,"test: Putting",len(piece_hashes),"into MerkleTree, size",self.tdef.get_piece_length(),tdef2.get_piece_length()
self.tree = MerkleTree(self.tdef.get_piece_length(),self.tdef.get_length(),None,piece_hashes)
f = open(self.sourcefn,"rb")
piece1 = f.read(2 ** 18)
piece2 = f.read(2 ** 18)
print >>sys.stderr,"read piece1",len(piece1)
print >>sys.stderr,"read piece2",len(piece2)
f.close()
hash1 = sha(piece1).digest()
hash2 = sha(piece2).digest()
print >>sys.stderr,"hash piece1",`hash1`
print >>sys.stderr,"hash piece2",`hash2`
f2 = open("piece1.bin","wb")
f2.write(piece2)
f2.close()
def setUpDownloadConfig(self):
dscfg = DownloadStartupConfig()
print >>sys.stderr,"test: Downloading to",self.config_path
dscfg.set_dest_dir(self.config_path)
dscfg.set_breakup_seed_bitfield(False)
return dscfg
def tearDown(self):
#.........这里部分代码省略.........