本文整理汇总了Python中BaseLib.Core.TorrentDef.TorrentDef.get_metainfo方法的典型用法代码示例。如果您正苦于以下问题:Python TorrentDef.get_metainfo方法的具体用法?Python TorrentDef.get_metainfo怎么用?Python TorrentDef.get_metainfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BaseLib.Core.TorrentDef.TorrentDef
的用法示例。
在下文中一共展示了TorrentDef.get_metainfo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestQueryReplyActive
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import get_metainfo [as 别名]
#.........这里部分代码省略.........
print >> sys.stderr,"test: waiting for reply"
s = self.openconn
msg = s.recv()
self.assert_(len(msg) > 0)
print >> sys.stderr,"test: Received overlay message",getMessageName(msg[0])
self.assert_(msg[0] == QUERY)
id = self.check_rquery(msg[1:])
resp = gen_qreply(id)
print >> sys.stderr,"test: sending QUERY_REPLY"
s.send(resp)
if good:
time.sleep(10)
# the other side should not have closed the connection, as
# this is all valid, so this should not throw an exception:
s.send('bla')
s.close()
else:
# the other side should not like this and close the connection
self.assert_(len(s.recv())==0)
s.close()
def create_good_simple_reply_dict(self,id):
r = {}
r['content_name'] = self.content_name
r['length'] = LENGTH
r['leecher'] = LEECHERS
r['seeder'] = SEEDERS
r['category'] = CATEGORY
# OLPROTO_PROTO_ELEVENTH
# set later r['torrent_size'] = 42
r['channel_permid'] = '$' * 83
r['channel_name'] = 'Nitin Channel'
d2 = {}
d2[self.tdef.get_infohash()] = r
d = {}
d['id'] = id
d['a'] = d2
return d
def create_good_simple_reply(self,id):
d = self.create_good_simple_reply_dict(id)
bmetainfo = bencode(self.tdef.get_metainfo())
d['a'][self.tdef.get_infohash()]['torrent_size'] = len(bmetainfo)
b = bencode(d)
return QUERY_REPLY+b
def create_good_simpleplustorrents_reply(self,id):
d = self.create_good_simple_reply_dict(id)
bmetainfo = bencode(self.tdef.get_metainfo())
d['a'][self.tdef.get_infohash()]['torrent_size'] = len(bmetainfo)
d['a'][self.tdef.get_infohash()]['metatype'] = 'application/x-tribler-stream'
d['a'][self.tdef.get_infohash()]['metadata'] = bmetainfo
b = bencode(d)
return QUERY_REPLY+b
def check_good_qreply(self,hits):
self.assert_(len(hits) == 1)
self.assert_(hits.keys()[0] == self.tdef.get_infohash())
hit = hits[self.tdef.get_infohash()]
self.assert_(hit['content_name'] == self.content_name)
self.assert_(hit['length'] == LENGTH)
self.assert_(hit['leecher'] == LEECHERS)
self.assert_(hit['seeder'] == SEEDERS)
self.assert_(hit['category'] == CATEGORY)
# OLPROTO_VERSION_ELEVENTH
bmetainfo = bencode(self.tdef.get_metainfo())
self.assert_(hit['torrent_size'] == len(bmetainfo))
if self.query.startswith('SIMPLE+METADATA'):
self.assert_(hit['metadata'] == bmetainfo)
def create_not_bdecodable(self,id):
return QUERY_REPLY+"bla"
def create_not_bdecodable_torrentfile(self,id):
d = self.create_good_simple_reply_dict(id)
d['a'][self.tdef.get_infohash()]['torrent_size'] = 3 # consistent with metadata. Should be named "metasize"
d['a'][self.tdef.get_infohash()]['metadata'] = 'bla'
b = bencode(d)
return QUERY_REPLY+b
def check_rquery(self,data):
d = bdecode(data)
self.assert_(type(d) == DictType)
self.assert_(d.has_key('q'))
q = d['q']
self.assert_(type(q) == StringType)
self.assert_(d.has_key('id'))
id = d['id']
self.assert_(type(id) == StringType)
self.assert_(q == self.query)
return d['id']
示例2: TestMagnetMiniBitTorrent
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import get_metainfo [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"]
示例3: setUpPostSession
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import get_metainfo [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()