本文整理汇总了Python中btconn.BTConnection类的典型用法代码示例。如果您正苦于以下问题:Python BTConnection类的具体用法?Python BTConnection怎么用?Python BTConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BTConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_bad
def _test_bad(self,gen_drequest_func):
options = '\x00\x00\x00\x00\x00\x10\x00\x00'
s = BTConnection('localhost',self.hisport,user_option_pattern=options,user_infohash=self.infohash)
print >> sys.stderr,"\ntest: ",gen_drequest_func
hsmsg = self.create_good_tribler_extend_hs_v2()
s.send(hsmsg)
msg = gen_drequest_func()
s.send(msg)
time.sleep(5)
# the other side should not like this and close the connection
try:
s.s.settimeout(10.0)
s.read_handshake_medium_rare(close_ok = True)
while True:
resp = s.recv()
if len(resp) > 0:
print >>sys.stderr,"test: Got",getMessageName(resp[0]),"from peer"
self.assert_(resp[0] == EXTEND or resp[0]==UNCHOKE)
else:
self.assert_(len(resp)==0)
s.close()
break
except socket.timeout:
print >> sys.stderr,"test: Timeout, bad, peer didn't close connection"
self.assert_(False)
示例2: test_all
def test_all(self):
"""
I want to start a Tribler client once and then connect to
it many times. So there must be only one test method
to prevent setUp() from creating a new client every time.
The code is constructed so unittest will show the name of the
(sub)test where the error occured in the traceback it prints.
"""
# myid needs to identify the connection as Tribler in order to
# get the 'same' bit added.f
myid = TRIBLER_PEERID_LETTER + "".join(map(chr, range(19)))
# Create a fake other client, so the EXTEND ut_pex won't be empty
msg2 = self.create_good_nontribler_extend_hs(listenport=4321)
s2 = BTConnection('localhost',self.hisport,mylistenport=4321,user_option_pattern='\x00\x00\x00\x00\x00\x10\x00\x00',user_infohash=self.infohash,myid=myid)
s2.read_handshake_medium_rare()
s2.send(msg2)
self.subtest_good_nontribler_ut_pex()
self.subtest_good_nontribler_ut_pex_diff_id()
self.subtest_good_tribler_ut_pex()
self.subtest_bad_ut_pex()
# now we add a second non-tribler peer. this peer should also
# be in the pex message but should not have the 'same' bit set
myid = "X" + "".join(map(chr, range(19)))
msg3 = self.create_good_nontribler_extend_hs(listenport=4322)
s3 = BTConnection('localhost',self.hisport,mylistenport=4322,user_option_pattern='\x00\x00\x00\x00\x00\x10\x00\x00',user_infohash=self.infohash,myid=myid)
s3.read_handshake_medium_rare()
s3.send(msg3)
self.subtest_good_nontribler_ut_pex_same_and_nonsame()
示例3: __init__
class ConnecterConnection:
def __init__(self,port):
self.s = BTConnection('localhost',port)
self.s.read_handshake_medium_rare()
self.connection = EncrypterConnection(self.s.get_his_id())
def get_my_id(self):
return self.s.get_my_id()
def get_unauth_peer_id(self):
return self.s.get_his_id()
def is_locally_initiated(self):
return True
def send_message(self,msg):
self.s.send(msg)
def get_message(self):
return self.s.recv()
def set_permid(self,x):
pass
def set_auth_peer_id(self,x):
pass
def close(self):
self.s.close()
示例4: _test_good
def _test_good(self,msg_gen_func,options=None,infohash=None):
if options is None and infohash is None:
s = BTConnection('localhost',self.hisport)
elif options is None:
s = BTConnection('localhost',self.hisport,user_infohash=infohash)
elif infohash is None:
s = BTConnection('localhost',self.hisport,user_option_pattern=options)
else:
s = BTConnection('localhost',self.hisport,user_option_pattern=options,user_infohash=infohash)
msg = msg_gen_func()
s.send(msg)
s.read_handshake_medium_rare()
time.sleep(5)
# Tribler should send an EXTEND message back
try:
s.s.settimeout(10.0)
resp = s.recv()
self.assert_(len(resp) > 0)
print >>sys.stderr,"test: Got reply",getMessageName(resp[0])
self.assert_(resp[0] == EXTEND)
self.check_tribler_extend_hs(resp[1:])
#s.close()
except socket.timeout:
print >> sys.stderr,"test: Timeout, bad, peer didn't reply with EXTEND message"
self.assert_(False)
示例5: singtest_connect_overlay
def singtest_connect_overlay(self):
"""
"""
# 1. Accept the data connection Tribler wants to establish with us
self.myss.settimeout(10.0)
conn, addr = self.myss.accept()
s = BTConnection('',0,conn,user_infohash=self.infohash,myid=self.myid)
s.read_handshake_medium_rare()
extmsg = self.create_good_tribler_extend_hs()
s.send(extmsg)
resp = s.recv()
self.assert_(len(resp) > 0)
print >> sys.stderr,"test: Data conn replies",getMessageName(resp[0])
# 2. Tribler should now try to establish an overlay connection with us
self.myss.settimeout(10.0)
conn, addr = self.myss.accept()
options = '\x00\x00\x00\x00\x00\x00\x00\x00'
s2 = OLConnection(self.my_keypair,'',0,conn,mylistenport=self.mylistenport)
# Desired behaviour is that the accept() succeeds. If not it will time
# out, and throw an exception, causing this test to fail.
time.sleep(3)
s.close()
s2.close()
示例6: __init__
def __init__(self,my_keypair,hostname,port,opensock=None,mylistenport=481,myoversion=None):
""" If opensock is not None, we assume this is a connection we
accepted, and he initiates the Challenge/Response
"""
self.my_keypair = my_keypair
self.b = BTConnection(hostname,port,opensock,mylistenport=mylistenport,myoversion=myoversion)
if opensock:
self.b.read_handshake_medium_rare()
# Read challenge
msg = self.b.recv()
assert(msg[0] == CHALLENGE)
randomB = bdecode(msg[1:])
[randomA,resp1_data] = self.create_good_response1(randomB,self.b.get_his_id())
self.b.send(resp1_data)
# Read response2
msg = self.b.recv()
assert(msg[0] == RESPONSE2)
else:
self.b.read_handshake()
[rB,chal_data] = self.create_good_challenge()
self.b.send(chal_data)
resp1_data = self.b.recv()
if DEBUG:
print >>sys.stderr,"olconn: recv",len(resp1_data),"bytes"
resp1_dict = bdecode(resp1_data[1:])
resp2_data = self.create_good_response2(rB,resp1_dict,self.b.get_his_id())
self.b.send(resp2_data)
if DEBUG:
print >>sys.stderr,"olconn: sent",len(resp2_data),"bytes"
示例7: subtest_good_drequest
def subtest_good_drequest(self):
"""
test good DIALBACK_REQUEST messages
"""
s = OLConnection(self.my_keypair,'localhost',self.hisport,mylistenport=self.mylistenport)
msg = self.create_good_drequest()
s.send(msg)
time.sleep(5)
# And connect back to us
conn, addr = self.myss.accept()
s2 = BTConnection('',0,conn,mylistenport=self.mylistenport,user_infohash=dialback_infohash)
s2.read_handshake_medium_rare()
resp = s2.recv()
print >> sys.stderr,"test: Me got DIALBACK_REPLY from him, len",len(resp)
self.assert_(resp[0] == DIALBACK_REPLY)
self.check_drequest(resp[1:])
示例8: _test_bad_challenge
def _test_bad_challenge(self, gen_chal_func):
s = BTConnection("localhost", self.hisport)
s.read_handshake()
[rB, chal_data] = gen_chal_func()
s.send(chal_data)
time.sleep(5)
# the other side should not like this and close the connection
msg = s.recv()
self.assert_(len(msg) == 0)
s.close()
示例9: subtest_old_tribler
def subtest_old_tribler(self):
# The tracker gives Tribler
try:
self.myss.settimeout(10.0)
conn, addr = self.myss.accept()
options = '\x00\x00\x00\x00\x00\x10\x00\x00'
s = BTConnection('',0,conn,user_option_pattern=options,user_infohash=self.infohash,myid=self.myid)
s.read_handshake_medium_rare()
# Seeing that we're an T<=3.5.0 peer, he should directly
# initiate an overlay conn with us
conn2, addr2 = self.myss.accept()
s2 = OLConnection(self.my_keypair,'',0,conn2,self.mylistenport)
time.sleep(5)
# 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()
s2.send('bla')
s2.close()
print >> sys.stderr,"test: Good, Tribler made overlay conn with us"
except socket.timeout:
print >> sys.stderr,"test: Bad, Tribler did not attempt to start an overlay conn with us"
self.assert_(False)
示例10: bad_request_and_disconnect
def bad_request_and_disconnect(self, payload):
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)
conn.send(EXTEND + chr(metadata_id) + bencode(payload))
self.read_extend_metadata_close(conn)
示例11: test_all
def test_all(self):
"""
I want to start a Tribler client once and then connect to
it many times. So there must be only one test method
to prevent setUp() from creating a new client every time.
The code is constructed so unittest will show the name of the
(sub)test where the error occured in the traceback it prints.
"""
# Create a fake other client, so the EXTEND ut_pex won't be empty
msg2 = self.create_good_nontribler_extend_hs(listenport=4321)
s2 = BTConnection('localhost',self.hisport,mylistenport=4321,user_option_pattern='\x00\x00\x00\x00\x00\x10\x00\x00',user_infohash=self.infohash)
s2.read_handshake_medium_rare()
s2.send(msg2)
self.subtest_good_nontribler_ut_pex()
self.subtest_good_nontribler_ut_pex_diff_id()
self.subtest_good_tribler_ut_pex()
self.subtest_bad_ut_pex()
示例12: _test_dreply
def _test_dreply(self,gen_dreply,good,diff_ips_test=False):
for i in range(self.NLISTENERS):
print >> sys.stderr,"test: waiting for #",i,"listenport",self.mylistenport[i]
conn, addr = self.myss[i].accept()
s = OLConnection(self.mykeypairs[i],'',0,conn,self.mylistenport[i])
while True:
msg = s.recv()
self.assert_(len(msg) > 0)
print >> sys.stderr,"test: Received overlay message",getMessageName(msg[0])
if msg[0] == DIALBACK_REQUEST:
break
self.assert_(msg[0] == DIALBACK_REQUEST)
self.check_drequest(msg[1:])
# Proper behaviour is to try to send a reply using a new return connection
s2 = BTConnection('localhost',self.hisport,mylistenport=self.mylistenport[i],user_infohash=dialback_infohash)
s2.read_handshake_medium_rare(close_ok = True)
if gen_dreply is not None:
resp = gen_dreply(i)
print >> sys.stderr,"test: sending DIALBACK_REPLY #",i
s2.send(resp)
time.sleep(2)
# the other side should always close the
# connection, either because we're done or he didn't like our
# bad DIALBACK_REPLY message
msg = s2.recv()
if len(msg) > 0:
print >> sys.stderr,"test: Received unexpected data",getMessageName(msg[0])
self.assert_(len(msg)==0)
s2.close()
# Not really necessary, but helps with test_dialback_active2
s.close()
ext_ip = self.session.get_external_ip()
print >>sys.stderr,"test: External IP address after test is",ext_ip
if diff_ips_test:
if self.config.sessconfig['dialback_trust_superpeers'] == 1:
good = True
else:
good = False
if good:
self.assert_(ext_ip == REPLY_IP)
else:
self.assert_(ext_ip == self.myoriginalip)
示例13: subtest_good_flood
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)
for counter in xrange(len(self.metadata_list) * 2):
piece = counter % len(self.metadata_list)
conn.send(self.create_good_extend_metadata_request(metadata_id, piece))
if counter > len(self.metadata_list):
self.read_extend_metadata_reject(conn, piece)
else:
self.read_extend_metadata_reply(conn, piece)
示例14: test_good_transfer
def test_good_transfer(self):
def torrentdef_retrieved(tdef):
tags["retrieved"].set()
tags["metainfo"] = tdef.get_metainfo()
tags = {"retrieved": threading.Event()}
assert TorrentDef.retrieve_from_magnet(self.create_good_url(), torrentdef_retrieved, timeout=60)
def do_supply():
# supply fake addresses (regular dht obviously wont work here)
ltmgr = LibtorrentMgr.getInstance()
for infohash in ltmgr.metainfo_requests:
handle = ltmgr.ltsession.find_torrent(lt.big_number(infohash.decode('hex')))
handle.connect_peer(("127.0.0.1", LISTEN_PORT), 0)
self.session.lm.rawserver.add_task(do_supply, delay=5.0)
# 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)
assert tags["retrieved"].wait(5)
assert tags["metainfo"]["info"] == self.tdef.get_metainfo()["info"]
示例15: test_good_transfer
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"]