Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# parse command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output.')
parser.add_argument('--url', dest='url', type=six.text_type, default=url,
help='The router URL (default: "ws://localhost:8080/ws").')
parser.add_argument('--realm', dest='realm', type=six.text_type, default=realm,
help='The realm to join (default: "realm1").')
args = parser.parse_args()
# start logging
if args.debug:
txaio.start_logging(level='debug')
else:
txaio.start_logging(level='info')
# any extra info we want to forward to our ClientSession (in self.config.extra)
extra = {
u'foobar': u'A custom value'
}
# now actually run a WAMP client using our session class ClientSession
runner = ApplicationRunner(url=args.url, realm=args.realm, extra=extra)
runner.run(ClientSession, auto_reconnect=True)
if USE_STREAMING_TESTEE:
self.setProtocolOptions(failByDrop=True) # needed for streaming mode
else:
# enable permessage-deflate WebSocket protocol extension
def accept(offers):
for offer in offers:
if isinstance(offer, PerMessageDeflateOffer):
return PerMessageDeflateOfferAccept(offer)
self.setProtocolOptions(perMessageCompressionAccept=accept)
if __name__ == '__main__':
txaio.start_logging(level='info')
factory = TesteeServerFactory(u"ws://127.0.0.1:9001")
reactor.listenTCP(9001, factory)
reactor.run()
# Crossbar.io connection configuration
url = os.environ.get('CBURL', u'wss://demo.crossbar.io/ws')
# url = os.environ.get('CBURL', u'192.168.1.142')
realm = os.environ.get('CBREALM', u'crossbardemo')
# parse command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output.')
parser.add_argument('--url', dest='url', type=six.text_type, default=url, help='The router URL (default: "ws://localhost:8080/ws").')
parser.add_argument('--realm', dest='realm', type=six.text_type, default=realm, help='The realm to join (default: "realm1").')
args = parser.parse_args()
if args.debug:
txaio.start_logging(level='debug')
else:
txaio.start_logging(level='info')
# custom configuration data
extra = {
# GPI pin of buzzer
u'button_pin': 18,
}
# create and start app runner for our app component ..
runner = ApplicationRunner(url=args.url, realm=args.realm, extra=extra)
runner.run(ButtonComponent, auto_reconnect=True)
# Crossbar.io connection configuration
url = os.environ.get('CBURL', u'wss://demo.crossbar.io/ws')
realm = os.environ.get('CBREALM', u'crossbardemo')
# parse command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output.')
parser.add_argument('--url', dest='url', type=six.text_type, default=url, help='The router URL (default: "ws://localhost:8080/ws").')
parser.add_argument('--realm', dest='realm', type=six.text_type, default=realm, help='The realm to join (default: "realm1").')
args = parser.parse_args()
# custom configuration data
if args.debug:
txaio.start_logging(level='debug')
else:
txaio.start_logging(level='info')
extra = {
u'led_count': 8, # Number of LED pixels.
u'led_pin': 12, # GPIO pin connected to the pixels (must support PWM!).
u'led_freq_hz': 800000, # LED signal frequency in hertz (usually 800khz)
u'led_dma': 5, # DMA channel to use for generating signal (try 5)
u'led_brightness': 255, # Set to 0 for darkest and 255 for brightest
u'led_invert': False, # True to invert the signal (when using NPN transistor level shift)
}
# create and start app runner for our app component ..
runner = ApplicationRunner(url=args.url, realm=args.realm, extra=extra)
runner.run(ColoramaDisplay, auto_reconnect=True)
:type components: Component or list of Components
:param log_level: a valid log-level (or None to avoid calling start_logging)
:type log_level: string
"""
# only for Twisted > 12
# ...so this isn't in all Twisted versions we test against -- need
# to do "something else" if we can't import .. :/ (or drop some
# support)
from twisted.internet.task import react
# actually, should we even let people "not start" the logging? I'm
# not sure that's wise... (double-check: if they already called
# txaio.start_logging() what happens if we call it again?)
if log_level is not None:
txaio.start_logging(level=log_level)
react(component._run, (components, ))
transport_factory = WampRawSocketClientFactory(create, serializer=self.serializer)
loop = asyncio.get_event_loop()
if logging_level == 'debug':
loop.set_debug(True)
txaio.use_asyncio()
txaio.config.loop = loop
if is_unix:
coro = loop.create_unix_connection(transport_factory, parsed_url.path)
else:
coro = loop.create_connection(transport_factory, parsed_url.hostname, parsed_url.port)
(_transport, protocol) = loop.run_until_complete(coro)
txaio.start_logging(level=logging_level) # @UndefinedVariable
try:
loop.add_signal_handler(signal.SIGTERM, loop.stop)
except NotImplementedError:
# signals are not available on Windows
pass
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# give Goodbye message a chance to go through, if we still
# have an active session
# it's not working now - because protocol is_closed must return Future
if protocol._session:
import txaio
from autobahn.twisted.websocket import WebSocketServerFactory, \
WebSocketServerProtocol, \
listenWS
class EchoServerProtocol(WebSocketServerProtocol):
def onMessage(self, payload, isBinary):
self.sendMessage(payload, isBinary)
if __name__ == '__main__':
txaio.start_logging(level='debug')
# SSL server context: load server key and certificate
# We use this for both WS and Web!
contextFactory = ssl.DefaultOpenSSLContextFactory('keys/server.key',
'keys/server.crt')
factory = WebSocketServerFactory(u"wss://127.0.0.1:9000")
# by default, allowedOrigins is "*" and will work fine out of the
# box, but we can do better and be more-explicit about what we
# allow. We are serving the Web content on 8080, but our WebSocket
# listener is on 9000 so the Origin sent by the browser will be
# from port 8080...
factory.setProtocolOptions(
allowedOrigins=[
"https://127.0.0.1:8080",
"https://localhost:8080",
:returns: None is returned, unless you specify
``start_reactor=False`` in which case the Deferred that
connect() returns is returned; this will callback() with
an IProtocol instance, which will actually be an instance
of :class:`WampWebSocketClientProtocol`
"""
self.log.debug('{klass}.run()', klass=self.__class__.__name__)
if start_reactor:
# only select framework, set loop and start logging when we are asked
# start the reactor - otherwise we are running in a program that likely
# already tool care of all this.
from twisted.internet import reactor
txaio.use_twisted()
txaio.config.loop = reactor
txaio.start_logging(level=log_level)
if callable(make):
# factory for use ApplicationSession
def create():
cfg = ComponentConfig(self.realm, self.extra, runner=self)
try:
session = make(cfg)
except Exception:
self.log.failure('ApplicationSession could not be instantiated: {log_failure.value}')
if start_reactor and reactor.running:
reactor.stop()
raise
else:
return session
else:
create = make
def run(self):
"""
start component
"""
self.loop = asyncio.get_event_loop()
if self.loop.is_closed():
asyncio.set_event_loop(asyncio.new_event_loop())
self.loop = asyncio.get_event_loop()
txaio.start_logging()
self.loop.run_until_complete(self.onConnect())
back to the caller.
:returns: None is returned, unless you specify
``start_reactor=False`` in which case the Deferred that
connect() returns is returned; this will callback() with
an IProtocol instance, which will actually be an instance
of :class:`WampWebSocketClientProtocol`
"""
if start_reactor:
# only select framework, set loop and start logging when we are asked
# start the reactor - otherwise we are running in a program that likely
# already tool care of all this.
from twisted.internet import reactor
txaio.use_twisted()
txaio.config.loop = reactor
txaio.start_logging(level=log_level)
if callable(make):
# factory for use ApplicationSession
def create():
cfg = ComponentConfig(self.realm, self.extra)
try:
session = make(cfg)
except Exception:
self.log.failure('ApplicationSession could not be instantiated: {log_failure.value}')
if start_reactor and reactor.running:
reactor.stop()
raise
else:
return session
else:
create = make