本文整理汇总了Python中BitTorrent.RawServer_twisted.RawServer类的典型用法代码示例。如果您正苦于以下问题:Python RawServer类的具体用法?Python RawServer怎么用?Python RawServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RawServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
class Network:
def __init__(self, size=0, startport=5555, localip='127.0.0.1'):
self.num = size
self.startport = startport
self.localip = localip
def _done(self, val):
self.done = 1
def simpleSetUp(self):
#self.kfiles()
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(Event(), d)
self.l = []
for i in range(self.num):
self.l.append(UTKhashmir('', self.startport + i, 'kh%s.db' % (self.startport + i), self.r))
for i in self.l:
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
self.r.listen_once(1)
self.r.listen_once(1)
self.r.listen_once(1)
for i in self.l:
self.done = 0
i.findCloseNodes(self._done)
while not self.done:
self.r.listen_once(1)
for i in self.l:
self.done = 0
i.findCloseNodes(self._done)
while not self.done:
self.r.listen_once(1)
def tearDown(self):
for i in self.l:
i.rawserver.stop_listening_udp(i.socket)
i.socket.close()
#self.kfiles()
def kfiles(self):
for i in range(self.startport, self.startport+self.num):
try:
os.unlink('kh%s.db' % i)
except:
pass
self.r.listen_once(1)
示例2: simpleSetUp
def simpleSetUp(self):
#self.kfiles()
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(Event(), d)
self.l = []
for i in range(self.num):
self.l.append(UTKhashmir('', self.startport + i, 'kh%s.db' % (self.startport + i), self.r))
for i in self.l:
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
i.addContact(self.localip, self.l[randrange(0,self.num)].port)
self.r.listen_once(1)
self.r.listen_once(1)
self.r.listen_once(1)
for i in self.l:
self.done = 0
i.findCloseNodes(self._done)
while not self.done:
self.r.listen_once(1)
for i in self.l:
self.done = 0
i.findCloseNodes(self._done)
while not self.done:
self.r.listen_once(1)
示例3: listen_forever
def listen_forever(self):
pool = NSAutoreleasePool.alloc().init()
# XXX
#self.profile = Profile("BT.prof");self.profile.start()
self.rawserver = RawServer(self.config)
self.mt = MultiTorrent(self.config, self.doneflag, self.rawserver, self.multi_errorfunc, self.config['data_dir'])
self.rawserver.ident = thread.get_ident()
self.mt.set_option("max_upload_rate", self.config['max_upload_rate'] * 1024)
self.rawserver.listen_forever(self.doneflag)
示例4: __init__
def __init__(self, host, port, data_dir, rawserver=None, max_ul_rate=1024, checkpoint=True, errfunc=None, rlcount=foo, config={'pause':False, 'max_rate_period':20}):
if rawserver:
self.rawserver = rawserver
else:
self.flag = Event()
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.rawserver = RawServer(self.flag, d)
self.max_ul_rate = max_ul_rate
self.socket = None
self.config = config
self.setup(host, port, data_dir, rlcount, checkpoint)
示例5: setUp
def setUp(self):
self.noisy = 0
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(d)
addr = ('127.0.0.1', 1180)
self.as = self.r.create_udpsocket(addr[1], addr[0], True)
self.af = Receiver(addr)
self.a = hostbroker(self.af, addr, self.as, self.r.add_task)
self.r.start_listening_udp(self.as, self.a)
addr = ('127.0.0.1', 1181)
self.bs = self.r.create_udpsocket(addr[1], addr[0], True)
self.bf = Receiver(addr)
self.b = hostbroker(self.bf, addr, self.bs, self.r.add_task)
self.r.start_listening_udp(self.bs, self.b)
示例6: main
def main(self):
"""\
Start the Mainline client and block indefinitely, listening for connectons.
"""
uiname = "bittorrent-console"
defaults = get_defaults(uiname)
config, args = configfile.parse_configuration_and_args(defaults, uiname)
config = Preferences().initWithDict(config)
data_dir = config['data_dir']
self.core_doneflag = DeferredEvent()
self.rawserver_doneflag = DeferredEvent()
rawserver = RawServer(config) #event and I/O scheduler
self.multitorrent = MultiTorrent(config, rawserver, data_dir) #class used to add, control and remove torrents
self.tick() #add periodic function call
rawserver.add_task(0, self.core_doneflag.addCallback, lambda r: rawserver.external_add_task(0, shutdown))
rawserver.listen_forever(self.rawserver_doneflag) # runs until the component terminates
self.send(producerFinished(self), "signal")
示例7: KRPCTests
class KRPCTests(TestCase):
def setUp(self):
self.noisy = 0
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(d)
addr = ('127.0.0.1', 1180)
self.as = self.r.create_udpsocket(addr[1], addr[0], True)
self.af = Receiver(addr)
self.a = hostbroker(self.af, addr, self.as, self.r.add_task)
self.r.start_listening_udp(self.as, self.a)
addr = ('127.0.0.1', 1181)
self.bs = self.r.create_udpsocket(addr[1], addr[0], True)
self.bf = Receiver(addr)
self.b = hostbroker(self.bf, addr, self.bs, self.r.add_task)
self.r.start_listening_udp(self.bs, self.b)
def tearDown(self):
self.as.close()
self.bs.close()
def testSimpleMessage(self):
self.noisy = 0
self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
self.r.listen_once(0.01)
self.assertEqual(self.bf.buf, ["This is a test."])
def testMessageBlast(self):
self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
self.r.listen_once(0.01)
self.assertEqual(self.bf.buf, ["This is a test."])
self.bf.buf = []
for i in range(100):
self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
self.r.listen_once(0.01)
#self.bf.buf = []
self.assertEqual(self.bf.buf, ["This is a test."] * 100)
def testEcho(self):
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is a test.")
def gotMsg(self, dict):
_krpc_sender = dict['_krpc_sender']
msg = dict['rsp']
self.msg = msg
def testManyEcho(self):
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is a test.")
for i in xrange(100):
self.msg = None
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is a test.")
def testMultiEcho(self):
self.noisy = 0
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is a test.")
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is another test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is another test.")
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is yet another test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is yet another test.")
def testEchoReset(self):
self.noisy = 0
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is a test.")
df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is another test."})
df.addCallback(self.gotMsg)
self.r.listen_once(0.01)
self.r.listen_once(0.01)
self.assertEqual(self.msg, "This is another test.")
#.........这里部分代码省略.........
示例8: setUp
def setUp(self):
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(d)
self.a = self.r.create_udpsocket(8051, '127.0.0.1')
self.b = self.r.create_udpsocket(8052, '127.0.0.1')
示例9: SimpleTests
class SimpleTests(TestCase):
def setUp(self):
d = dict([(x[0],x[1]) for x in common_options + rare_options])
self.r = RawServer(d)
self.a = self.r.create_udpsocket(8051, '127.0.0.1')
self.b = self.r.create_udpsocket(8052, '127.0.0.1')
def tearDown(self):
self.a.close()
self.b.close()
def Handler(self, expected):
class h(object):
def __init__(self, expected, a=self.assertEqual):
self.expected = expected
self.a = a
def data_came_in(self, connection, data):
self.a(self.expected, data)
return h(expected)
def testFoo(self):
self.r.start_listening_udp(self.a, self.Handler(''))
self.r.start_listening_udp(self.b, self.Handler('foo'))
self.a.sendto("foo", 0, ('127.0.0.1', 8052))
self.r.listen_once()
def testBackForth(self):
self.r.start_listening_udp(self.a, self.Handler('bar'))
self.r.start_listening_udp(self.b, self.Handler('foo'))
self.a.sendto("foo", 0, ('127.0.0.1', 8052))
self.r.listen_once()
self.b.sendto("bar", 0, ('127.0.0.1', 8051))
self.r.listen_once()
示例10: __init__
def __init__(self, config, display, configfile_key):
"""Starts torrents for all .torrent files in a directory tree.
All errors are logged using Python logging to 'configfile_key' logger.
@param config: Preferences object storing config.
@param display: output function for stats.
"""
# 4.4.x version of LaunchMany output exceptions to a displayer.
# This version only outputs stats to the displayer. We do not use
# the logger to output stats so that a caller-provided object
# can provide stats formatting as opposed to using the
# logger Formatter, which is specific to exceptions, warnings, and
# info messages.
self.logger = logging.getLogger(configfile_key)
try:
self.multitorrent = None
self.rawserver = None
self.config = config
self.configfile_key = configfile_key
self.display = display
self.torrent_dir = config['torrent_dir']
# Ex: torrent_cache = infohash -> (path,metainfo)
self.torrent_cache = {}
# maps path -> [(modification time, size), infohash]
self.file_cache = {}
# used as set containing paths of files that do not have separate
# entries in torrent_cache either because torrent_cache already
# contains the torrent or because the torrent file is corrupt.
self.blocked_files = {}
#self.torrent_list = []
#self.downloads = {}
self.hashcheck_queue = []
#self.hashcheck_store = {}
self.hashcheck_current = None
self.core_doneflag = DeferredEvent()
self.rawserver = RawServer(self.config)
try:
# set up shut-down procedure before we begin doing things that
# can throw exceptions.
def shutdown():
self.logger.critical(_("shutting down"))
for t in self.multitorrent.get_torrents():
self.logger.info(_('dropped "%s"') %
self.torrent_cache[t.infohash][0])
if self.multitorrent:
df = self.multitorrent.shutdown()
set_flag = lambda *a : self.rawserver.stop()
df.addCallbacks(set_flag, set_flag)
else:
self.rawserver.stop()
# It is safe to addCallback here, because there is only one thread,
# but even if the code were multi-threaded, core_doneflag has not
# been passed to anyone. There is no chance of a race condition
# between the DeferredEvent's callback and addCallback.
self.core_doneflag.addCallback(
lambda r: self.rawserver.external_add_task(0, shutdown))
self.rawserver.install_sigint_handler(self.core_doneflag)
data_dir = config['data_dir']
self.multitorrent = MultiTorrent(config, self.rawserver, data_dir,
resume_from_torrent_config=False)
self.rawserver.add_task(0, self.scan)
self.rawserver.add_task(0.5, self.periodic_check_hashcheck_queue)
self.rawserver.add_task(self.config['display_interval'],
self.periodic_stats)
try:
import signal
def handler(signum, frame):
self.rawserver.external_add_task(0, self.read_config)
signal.signal(signal.SIGHUP, handler)
except Exception, e:
self.logger.error(_("Could not set signal handler: ") +
str_exc(e))
self.rawserver.add_task(0,self.core_doneflag.set())
except UserFailure, e:
self.logger.error(str_exc(e))
self.rawserver.add_task(0,self.core_doneflag.set())
except:
data = StringIO()
print_exc(file = data)
self.logger.error(data.getvalue())
self.rawserver.add_task(0,self.core_doneflag.set())
# always make sure events get processed even if only for
# shutting down.
#.........这里部分代码省略.........
示例11: RawServer
try:
import hotshot
import hotshot.stats
prof_file_name = 'NewRateLimiter.prof'
except ImportError, e:
print "profiling not available:", e
profile = False
import random
from BitTorrent.RawServer_twisted import RawServer
from twisted.internet import task
from BitTorrent.defer import DeferredEvent
config = {}
rawserver = RawServer(config)
doneflag = DeferredEvent()
s = Scheduler(4096, add_task = rawserver.add_task)
s.unitsize = 17000
a = []
for i in xrange(500):
keyable = FakeConnection(global_rate)
a.append(keyable)
freq = 0.01
def push():
if random.randint(0, 5 / freq) == 0:
示例12: RawServer
from BitTorrent.RawServer_twisted import RawServer
from twisted.internet import defer
from twisted.internet import reactor
config = {'max_incomplete': 10,
'max_upload_rate': 350000,
'bind': '',
'close_with_rst': False,
'socket_timeout': 3000}
m = 20000
a = [None] * m
i = 0
if __name__ == '__main__':
rawserver = RawServer(config=config, noisy=True)
rawserver.install_sigint_handler()
def avg_dist(l):
d = []
p = None
for x in l:
if p is not None:
d.append(x - p)
p = x
return sum(d) / len(d)
def rerun():
#print '.'
global i
示例13: message_dump
def message_dump(data):
print "Received: ", data.encode("hex")
def shutdown():
print "shutdown."
rawserver.stop()
if __name__ == "__main__":
print "test2"
sys.stdout.flush()
uiname = "bittorrent-console"
defaults = get_defaults(uiname)
config, args = configfile.parse_configuration_and_args(defaults,
uiname, sys.argv[1:], 0, 1)
core_doneflag = DeferredEvent()
rawserver = RawServer(config)
core_doneflag.addCallback(
lambda r: rawserver.external_add_task(0, shutdown))
rawserver.install_sigint_handler(core_doneflag)
print "Creating IcmpIPC"
icmp = IcmpIPC(rawserver)
icmp.create()
print "Starting IcmpIPC"
icmp.start(message_dump)
rawserver.listen_forever()
示例14: BTAppController
class BTAppController (NibClassBuilder.AutoBaseClass):
def init(self):
self = super(BTAppController, self).init()
self.prefs = Preferences.alloc().init()
self.prefwindow = None
self.generator = Generate.alloc().init()
self.ic =ICHelper.alloc().init()
# displayed torrent controllers
self.torrents = []
# waiting to die
self.dead_torrents = []
# ready to start
# (<open panel>, <insert row>, (<filename>|<stream>, <is_stream>)) -1 insert row means use last row
# stream = 0 if filename, 1 if bencoded torrent file
self.tqueue = []
self.retain()
self.inited = 0
self.launched = 0
self.in_choose = 0
self.last_qcheck = time()
self.sc = 0
self.defaults = NSUserDefaults.standardUserDefaults()
self.tup = bdecode(self.defaults.objectForKey_(ULBYTES))
self.tdown = bdecode(self.defaults.objectForKey_(DLBYTES))
self.config = common_options + rare_options
self.config = BTPreferences().initWithDict(dict([(name, value) for (name, value, doc) in self.config]))
self.config['data_dir'] = PREFDIR
self.config['bind'] = ''
self.config['bad_libc_workaround'] = True
self.config['filesystem_encoding'] = 'utf8'
#XXXX
#self.config['start_trackerless_client'] = False
self.legacyConfig()
self.reloadConfig()
self.pyi = None
self.doneflag = Event()
if not os.path.exists(PREFDIR):
os.mkdir(PREFDIR)
if not os.path.exists(RESDIR):
os.mkdir(RESDIR)
self.ticon = None
self.stalled = []
self.terminated = False
return self
def loadConsole_(self, sender):
if not self.pyi:
self.pyi = PyInterpreter.alloc().init()
NSBundle.loadNibNamed_owner_("PyInterpreter", self.pyi)
else:
self.pyi.textView.window().makeKeyAndOrderFront_(self)
def legacyConfig(self):
n = self.defaults.integerForKey_(QUEUESTOP)
if n > 1:
self.defaults.setObject_forKey_(0, QUEUESTOP)
def reloadConfig(self):
self.config['minport'] = self.defaults.integerForKey_(MINPORT)
self.config['maxport'] = self.defaults.integerForKey_(MINPORT)
self.config['max_upload_rate'] = self.defaults.integerForKey_(MAXULRATE)
self.config['max_allow_in'] = self.defaults.integerForKey_(MAXACCEPT)
self.config['max_initiate'] = self.defaults.integerForKey_(MAXINITIATE)
self.config['max_uploads'] = self.defaults.integerForKey_(MAXULS)
def listen_forever(self):
pool = NSAutoreleasePool.alloc().init()
# XXX
#self.profile = Profile("BT.prof");self.profile.start()
self.rawserver = RawServer(self.config)
self.mt = MultiTorrent(self.config, self.doneflag, self.rawserver, self.multi_errorfunc, self.config['data_dir'])
self.rawserver.ident = thread.get_ident()
self.mt.set_option("max_upload_rate", self.config['max_upload_rate'] * 1024)
self.rawserver.listen_forever(self.doneflag)
#self.profile.stop();self.profile.close()
def multi_errorfunc(self, level, text ):
if level == CRITICAL:
self.statusField.setStringValue_(NSLocalizedString("Critical Error: %s", "critical error") % text)
# bomb out
elif level == ERROR:
self.statusField.setStringValue_(NSLocalizedString("Error: %s", "normal error") % text)
#.........这里部分代码省略.........
示例15: print_list
print_list(list)
df = nattraverser.register_port(internal_port, internal_port, "TCP")
external_port = like_yield(df)
print "Mapped:", external_port
list = like_yield(nattraverser.list_ports())
print_list(list)
# synchronous
nattraverser.unregister_port(external_port, "TCP")
list = like_yield(nattraverser.list_ports())
print_list(list)
if __name__ == "__main__":
rawserver = RawServer({'upnp': True})
nattraverser = NatTraverser(rawserver)
port = 6881
if len(sys.argv) > 1:
port = int(sys.argv[1])
df = run_tests(port)
def error(f):
print f
df.addErrback(error)
df.addBoth(lambda r: rawserver.stop())
rawserver.listen_forever()