本文整理汇总了Python中autobahn.asyncio.websocket.WebSocketServerFactory方法的典型用法代码示例。如果您正苦于以下问题:Python websocket.WebSocketServerFactory方法的具体用法?Python websocket.WebSocketServerFactory怎么用?Python websocket.WebSocketServerFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autobahn.asyncio.websocket
的用法示例。
在下文中一共展示了websocket.WebSocketServerFactory方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def __init__(self, hostname="localhost", port="8080", processor=None):
self.hostname = hostname
self.port = port
self.processor = processor
factory = WebSocketServerFactory(
u"ws://" + hostname + u":" + str(port))
protocol = EventProtocol
protocol.processor = processor
protocol.app = self
factory.protocol = protocol
self.loop = asyncio.get_event_loop()
self.server = self.loop.create_server(factory, '0.0.0.0', port)
示例2: startTwisted
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def startTwisted(self):
from twisted.python import log
log.startLogging(open("wssserver.log", "w"))
self.print_debug("startTwisted() started")
ws = "ws"
ResourceProtocol.server = self
sslcontext = None
if self.ssl:
self.print_debug("using wss... and ssl")
sslcontext = ssl.DefaultOpenSSLContextFactory(self.sslkey, self.sslcert)
ws = "wss"
factory = WebSocketServerFactory(u"{}://127.0.0.1:{}".format(ws, self.port))
factory.protocol = ResourceProtocol
listenWS(factory, sslcontext)
reactor.run()
示例3: __init__
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def __init__(self, singleclient=True, hostname="127.0.0.1", address="0.0.0.0", port=9000, protocol=SofiEventProtocol):
# General application configuration info
self.hostname = hostname
self.address = address
self.port = port
# Protocol class that will manage messaging
self.protocol = protocol
protocol.app = self
# Event handlers
self.handlers = {
'init': {'_': set()},
'load': {'_': set()},
'close': {'_': set()},
'click': {'_': set()},
'mousedown': {'_': set()},
'mouseup': {'_': set()},
'keydown': {'_': set()},
'keyup': {'_': set()},
'keypress': {'_': set()}
}
# Client management
self.clients = list()
self.singleclient = singleclient
# Create the factory that generates protocols to handle socket communications
factory = WebSocketServerFactory("ws://" + hostname + ":" + str(port))
factory.protocol = protocol
# Create the Asyncio event loop
self.loop = asyncio.get_event_loop()
# Create the loop server
self.server = self.loop.create_server(factory, address, port)
示例4: test_websocket_custom_loop
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def test_websocket_custom_loop(self):
def time_gen():
yield
yield
loop = AsyncioTestLoop(time_gen)
factory = WebSocketServerFactory(loop=loop)
server = factory()
transport = Mock()
server.connection_made(transport)
# not sure when this last worked, tests haven't been running
# properly under asyncio for a while it seems.
示例5: start
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def start(self):
self.print_debug("start() called... debug = {}".format(self.debug))
ws = "ws"
sslcontext = None
if self.ssl:
self.print_debug("using ssl")
try:
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext.load_cert_chain(self.sslcert, self.sslkey)
self.print_debug("using ssl")
except:
sslcontext = None
self.print_debug("failed to use ssl")
ws = "wss"
ResourceProtocol.server = self
factory = WebSocketServerFactory(u"{0}://127.0.0.1:{1}".format(ws, self.port))
factory.protocol = ResourceProtocol
loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '', self.port, ssl=sslcontext)
self.server = loop.run_until_complete(coro)
self.print_debug("server should be started now")
示例6: __init__
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def __init__(self, hostname=u"127.0.0.1", port=9000, processor=None):
self.hostname = hostname
self.port = port
self.processor = processor
factory = WebSocketServerFactory(u"ws://" + hostname + u":" + str(port))
protocol = FBWEventProtocol
protocol.processor = processor
protocol.app = self
factory.protocol = protocol
self.loop = asyncio.get_event_loop()
self.server = self.loop.create_server(factory, '0.0.0.0', port)
示例7: test_async_on_connect_server
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def test_async_on_connect_server(self):
# see also issue 757
# for python 3.5, this can be "async def foo"
def foo(x):
f = txaio.create_future()
txaio.resolve(f, x * x)
return f
values = []
def on_connect(req):
f = txaio.create_future()
def cb(x):
f = foo(42)
f.add_callbacks(f, lambda v: values.append(v), None)
return f
txaio.add_callbacks(f, cb, None)
return f
factory = WebSocketServerFactory()
server = factory()
server.onConnect = on_connect
transport = Mock()
server.connection_made(transport)
# need/want to insert real-fake handshake data?
server.data = b"\r\n".join([
b'GET /ws HTTP/1.1',
b'Host: www.example.com',
b'Sec-WebSocket-Version: 13',
b'Origin: http://www.example.com.malicious.com',
b'Sec-WebSocket-Extensions: permessage-deflate',
b'Sec-WebSocket-Key: tXAxWFUqnhi86Ajj7dRY5g==',
b'Connection: keep-alive, Upgrade',
b'Upgrade: websocket',
b'\r\n', # last string doesn't get a \r\n from join()
])
server.processHandshake()
import asyncio
from asyncio.test_utils import run_once
run_once(asyncio.get_event_loop())
self.assertEqual(1, len(values))
self.assertEqual(42 * 42, values[0])
示例8: __init__
# 需要导入模块: from autobahn.asyncio import websocket [as 别名]
# 或者: from autobahn.asyncio.websocket import WebSocketServerFactory [as 别名]
def __init__(self, settings):
coro = None
self._started = False
self._settings = settings
self._spine = Spine()
self._spine.register_event_handler("dynamicValueChanged", None)
try:
import asyncio
except ImportError:
## Trollius >= 0.3 was renamed
import trollius as asyncio
from autobahn.asyncio.websocket import WebSocketServerFactory
ssl_context = None
if encryption.enabled():
print("socket using ssl")
cert_file, key_file = encryption.get_cert()
try:
import ssl
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.load_cert_chain(cert_file, key_file)
print("socket ssl found")
except:
ssl_context = None
print("socket failed to use ssl")
self._spine.log.debug(
"start websocket on:{0}, port:{1}",
self._settings["network"]["IPAddress"],
self._settings["network"]["WebSocketPort"]
)
self.factory = WebSocketServerFactory()
self.factory.protocol = _SpineProtocol
self.loop = asyncio.get_event_loop()
self.coro = self.loop.create_server(
self.factory,
self._settings["network"]["IPAddress"],
self._settings["network"]["WebSocketPort"],
ssl=ssl_context
)