本文整理汇总了Python中twisted.internet.selectreactor.SelectReactor.run方法的典型用法代码示例。如果您正苦于以下问题:Python SelectReactor.run方法的具体用法?Python SelectReactor.run怎么用?Python SelectReactor.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.selectreactor.SelectReactor
的用法示例。
在下文中一共展示了SelectReactor.run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_uninvited_pubrel
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_uninvited_pubrel(host, port):
record = [
Frame(
send=True,
data=Connect(client_id=u"test_pubrel",
flags=ConnectFlags(clean_session=True))),
Frame(
send=False,
data=ConnACK(session_present=False, return_code=0)),
Frame(
send=True,
data=PubREL(packet_identifier=1234)),
Frame(
send=False,
data=PubCOMP(packet_identifier=1234)),
Frame(
send=True,
data=Disconnect()),
ConnectionLoss(),
]
r = SelectReactor()
f = ReplayClientFactory(r, record)
e = TCP4ClientEndpoint(r, host, port)
e.connect(f)
r.run()
return Result("uninvited_pubrel", f.success, f.reason, f.client_transcript)
示例2: test_basic_subresources
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_basic_subresources(self):
"""
A basic WSGI app can be ran, with subresources
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"hello",
"type": u"wsgi"
},
u"json": {
"type": u"json",
"value": {}
}
}
})
# Make a request to the /json endpoint, which is technically a child of
# the WSGI app, but is not served by WSGI.
d = treq.get("http://localhost:8080/json", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(self.assertEqual, b"{}")
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
return d
示例3: test_basic
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_basic(self):
"""
A basic WSGI app can be ran.
"""
temp_reactor = SelectReactor()
r = router.RouterController(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
u"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"hello",
"type": u"wsgi"
}
}
})
# Make a request to the WSGI app.
d = treq.get("http://localhost:8080/", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(self.assertEqual, b"hello!")
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
return d
示例4: test_qos2_send_wrong_confirm
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_qos2_send_wrong_confirm(host, port):
record = [
Frame(
send=True,
data=Connect(client_id=u"test_wrong_confirm_qos2",
flags=ConnectFlags(clean_session=True))),
Frame(
send=False,
data=ConnACK(session_present=False, return_code=0)),
Frame(
send=True,
data=Subscribe(packet_identifier=1234,
topic_requests=[SubscriptionTopicRequest(u"foo", 2)])),
Frame(
send=False,
data=SubACK(packet_identifier=1234, return_codes=[2])),
Frame(
send=True,
data=Publish(duplicate=False, qos_level=2, topic_name=u"foo",
payload=b"abc", retain=False, packet_identifier=12)),
Frame(
send=False,
data=[
PubREC(packet_identifier=12),
Publish(duplicate=False, qos_level=2, topic_name=u"foo",
payload=b"abc", retain=False, packet_identifier=1),
PubCOMP(packet_identifier=12)]),
Frame(
send=True,
data=PubREL(packet_identifier=12)),
Frame(
send=True,
data=PubACK(packet_identifier=1)),
Frame(
send=False,
data=b""),
Frame(
send=True,
data=Disconnect()),
ConnectionLoss(),
]
r = SelectReactor()
f = ReplayClientFactory(r, record)
e = TCP4ClientEndpoint(r, host, port)
e.connect(f)
r.run()
return Result("qos2_wrong_confirm", f.success, f.reason, f.client_transcript)
示例5: __init__
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
class ReactorSpinController:
STATE_STOPPED = 0
STATE_STARTED = 1
STATE_SHUTTING_DOWN = 2
def __init__(self, evManager):
self.state = ReactorSpinController.STATE_STOPPED
self.evManager = evManager
self.evManager.RegisterListener( self )
self.reactor = SelectReactor()
installReactor(self.reactor)
self.loopingCall = LoopingCall(self.FireTick)
#----------------------------------------------------------------------
def FireTick(self):
self.evManager.Post( TickEvent() )
#----------------------------------------------------------------------
def Run(self):
self.state = ReactorSpinController.STATE_STARTED
framesPerSecond = 10
interval = 1.0 / framesPerSecond
self.loopingCall.start(interval)
self.reactor.run()
#----------------------------------------------------------------------
def Stop(self):
print 'stopping the reactor'
self.state = ReactorSpinController.STATE_SHUTTING_DOWN
self.reactor.addSystemEventTrigger('after', 'shutdown',
self.onReactorStop)
self.reactor.stop()
#----------------------------------------------------------------------
def onReactorStop(self):
print 'reactor is now totally stopped'
self.state = ReactorSpinController.STATE_STOPPED
self.reactor = None
#----------------------------------------------------------------------
def Notify(self, event):
if isinstance( event, QuitEvent ):
self.Stop()
示例6: test_quirks_mode_connect
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_quirks_mode_connect(host, port):
record = [
Frame(
send=True,
data=b"\x10\x15\x00\x04MQTT\x04\x02\x00x\x00\x07testqrk\x00\x00"),
Frame(
send=False,
data=ConnACK(session_present=False, return_code=0)),
Frame(
send=True,
data=Disconnect()),
ConnectionLoss(),
]
r = SelectReactor()
f = ReplayClientFactory(r, record)
e = TCP4ClientEndpoint(r, host, port)
e.connect(f)
r.run()
return Result("connect_quirks", f.success, f.reason, f.client_transcript)
示例7: test_connect
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_connect(host, port):
record = [
Frame(
send=True,
data=Connect(client_id=u"test_cleanconnect",
flags=ConnectFlags(clean_session=True))),
Frame(
send=False,
data=ConnACK(session_present=False, return_code=0)),
Frame(
send=True,
data=Disconnect()),
ConnectionLoss(),
]
r = SelectReactor()
f = ReplayClientFactory(r, record)
e = TCP4ClientEndpoint(r, host, port)
e.connect(f)
r.run()
return Result("connect", f.success, f.reason, f.client_transcript)
示例8: test_reserved_packet_15
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_reserved_packet_15(host, port):
record = [
Frame(
send=True,
data=Connect(client_id=u"test_reserved15",
flags=ConnectFlags(clean_session=True))),
Frame(
send=False,
data=ConnACK(session_present=False, return_code=0)),
Frame(
send=True,
# v pkt 15 right here
data=b"\xf0\x13\x00\x04MQTT\x04\x02\x00\x02\x00\x07test123"),
ConnectionLoss()
]
r = SelectReactor()
f = ReplayClientFactory(r, record)
e = TCP4ClientEndpoint(r, host, port)
e.connect(f)
r.run()
return Result("reserved_pkt15", f.success, f.reason, f.client_transcript)
示例9: test_start
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_start(self):
"""
A basic start, that doesn't actually enter the reactor.
"""
with open(self.config, "w") as f:
f.write("""{"controller": {}}""")
reactor = SelectReactor()
reactor.run = lambda: False
cli.run("crossbar",
["start", "--cbdir={}".format(self.cbdir),
"--logformat=syslogd"],
reactor=reactor)
self.assertIn("Entering reactor event loop", self.stdout.getvalue())
示例10: test_fileLogging
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_fileLogging(self):
"""
Running `crossbar start --logtofile` will log to cbdir/node.log.
"""
with open(self.config, "w") as f:
f.write("""{"controller": {}}""")
reactor = SelectReactor()
reactor.run = lambda: None
cli.run("crossbar",
["start", "--cbdir={}".format(self.cbdir), "--logtofile"],
reactor=reactor)
with open(os.path.join(self.cbdir, "node.log"), "r") as f:
logFile = f.read()
self.assertIn("Entering reactor event loop", logFile)
self.assertEqual("", self.stderr.getvalue())
self.assertEqual("", self.stdout.getvalue())
示例11: test_stalePID
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_stalePID(self):
with open(self.config, "w") as f:
f.write("""{"controller": {}}""")
with open(os.path.join(self.cbdir, "node.pid"), "w") as f:
f.write("""{"pid": 9999999}""")
reactor = SelectReactor()
reactor.run = lambda: None
cli.run("crossbar",
["start", "--cbdir={}".format(self.cbdir),
"--logformat=syslogd"],
reactor=reactor)
self.assertIn(
("Stale Crossbar.io PID file (pointing to non-existing process "
"with PID {pid}) {fp} removed").format(
fp=os.path.abspath(os.path.join(self.cbdir, "node.pid")),
pid=9999999),
self.stdout.getvalue())
示例12: test_root_not_required
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_root_not_required(self):
"""
Not including a '/' path will mean that path has a 404, but children
will still be routed correctly.
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
# Make a file
with open(os.path.join(self.cbdir, 'file.txt'), "wb") as f:
f.write(b"hello!")
r.start_router_realm(u"realm1", realm_config)
r.start_router_transport(
u"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"static": {
u"directory": u".",
u"type": u"static"
}
}
})
d1 = treq.get("http://localhost:8080/", reactor=temp_reactor)
d1.addCallback(lambda resp: self.assertEqual(resp.code, 404))
d2 = treq.get("http://localhost:8080/static/file.txt",
reactor=temp_reactor)
d2.addCallback(treq.content)
d2.addCallback(self.assertEqual, b"hello!")
def done(results):
for item in results:
if not item[0]:
return item[1]
d = defer.DeferredList([d1, d2])
d.addCallback(done)
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()
示例13: NetworkServerView
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
class NetworkServerView(pb.Root):
"""We SEND events to the server through this object"""
STATE_PREPARING = 0
STATE_CONNECTING = 1
STATE_CONNECTED = 2
def __init__(self, evm, shared_obj_registry):
self.evm = evm
self.evm.register(self)
self.pbClientFactory = pb.PBClientFactory()
self.reactor = None
self.server = None
self.shared_objs = shared_obj_registry
self.state = NetworkServerView.STATE_PREPARING
def connected(self, server):
print 'Connection to %s %s successful' %SERVERHOST, SERVERPORT
self.state = NetworkServerView.STATE_CONNECTED
self.server = server
self.evm.post(ev.ServerConnected(server))
def connection_failed(self, error):
print "Connection failed with error ", error
def attempt_connection(self):
print "Attempting connection to", SERVERHOST, SERVERPORT
self.state = NetworkServerView.STATE_CONNECTING
if self.reactor:
self.reactor.stop()
self.pump_reactor()
else:
self.reactor = SelectReactor()
installReactor(self.reactor)
connection = self.reactor.connectTCP(SERVERHOST, SERVERPORT,
self.pbClientFactory)
deferred = self.pbClientFactory.getRootObject()
deferred.addCallback(self.connected)
deferred.addErrback(self.connection_failed)
self.reactor.run()
def pump_reactor(self):
self.reactor.runUntilCurrent()
self.reactor.doIteration(0)
def disconnect(self):
print "disconnecting"
def notify(self, evt):
if evt.id == 'Tick':
if self.state == NetworkServerView.STATE_PREPARING:
self.attempt_connection()
elif self.state in [NetworkServerView.STATE_CONNECTING]:
self.pump_reactor()
示例14: SelectReactor
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
from twisted.internet.selectreactor import SelectReactor
from twisted.internet.protocol import Protocol, ClientFactory
reactor = SelectReactor()
protocol = Protocol()
class QuickDisconnectedProtocol(Protocol):
def connectionMade(self):
print "Connected to %s."%self.transport.getPeer().host
self.transport.loseConnection()
class BasicClientFactory(ClientFactory):
protocol = QuickDisconnectedProtocol
def clientConnectionLost(self, connector, reason):
print 'Lost connection:%s'%reason.getErrorMessage()
reactor.stop()
def clientConnectionFailed(self,connector,reason):
print 'Connection failed: %s'%reason.getErrorMessage()
reactor.stop()
reactor.connectTCP('www.google.com',80,BasicClientFactory())
reactor.run()
示例15: test_threads
# 需要导入模块: from twisted.internet.selectreactor import SelectReactor [as 别名]
# 或者: from twisted.internet.selectreactor.SelectReactor import run [as 别名]
def test_threads(self):
"""
A basic WSGI app can be ran, with subresources
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
threads = 20
r.start_router_realm("realm1", realm_config)
r.start_router_transport(
"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"/": {
"module": u"crossbar.worker.test.test_router",
"object": u"sleep",
"type": u"wsgi",
"maxthreads": threads,
}
}
})
deferreds = []
results = []
for i in range(threads):
d = treq.get("http://localhost:8080/", reactor=temp_reactor)
d.addCallback(treq.content)
d.addCallback(results.append)
deferreds.append(d)
def done(_):
max_concurrency = max([int(x) for x in results])
assert max_concurrency == threads, "Maximum concurrency was %s, not %s" % (max_concurrency, threads)
temp_reactor.stop()
defer.DeferredList(deferreds).addCallback(done)
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()