本文整理汇总了Python中autobahn.twisted.websocket.WampWebSocketServerProtocol类的典型用法代码示例。如果您正苦于以下问题:Python WampWebSocketServerProtocol类的具体用法?Python WampWebSocketServerProtocol怎么用?Python WampWebSocketServerProtocol使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WampWebSocketServerProtocol类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connectionLost
def connectionLost(self, reason):
try:
log.msg("Connection to node controller lost.")
WampWebSocketServerProtocol.connectionLost(self, reason)
except:
pass
finally:
## loosing the connection to the node controller (the pipes) is fatal.
## stop the reactor and exit with error
if reactor.running:
reactor.addSystemEventTrigger('after', 'shutdown', os._exit, 1)
reactor.stop()
else:
sys.exit(1)
示例2: connectionLost
def connectionLost(self, reason):
try:
# this log message is unlikely to reach the controller (unless
# only stdin/stdout pipes were lost, but not stderr)
log.msg("Connection to node controller lost.")
WampWebSocketServerProtocol.connectionLost(self, reason)
except:
pass
finally:
# loosing the connection to the node controller is fatal:
# stop the reactor and exit with error
if reactor.running:
reactor.addSystemEventTrigger('after', 'shutdown', os._exit, 1)
reactor.stop()
示例3: connectionLost
def connectionLost(self, reason):
# the behavior here differs slightly whether we're shutting down orderly
# or shutting down because of "issues"
if isinstance(reason.value, ConnectionDone):
was_clean = True
else:
was_clean = False
try:
# this log message is unlikely to reach the controller (unless
# only stdin/stdout pipes were lost, but not stderr)
if was_clean:
log.info("Connection to node controller closed cleanly")
else:
log.warn("Connection to node controller lost: {reason}", reason=reason)
# give the WAMP transport a change to do it's thing
WampWebSocketServerProtocol.connectionLost(self, reason)
except:
# we're in the process of shutting down .. so ignore ..
pass
finally:
# after the connection to the node controller is gone,
# the worker is "orphane", and should exit
# determine process exit code
if was_clean:
exit_code = 0
else:
exit_code = 1
# exit the whole worker process when the reactor has stopped
reactor.addSystemEventTrigger('after', 'shutdown', os._exit, exit_code)
# stop the reactor
try:
reactor.stop()
except ReactorNotRunning:
pass
示例4: onConnect
def onConnect(self, request):
protocol, headers = WampWebSocketServerProtocol.onConnect(self, request)
# our cookie tracking ID
self._cbtid = None
# see if there already is a cookie set ..
if 'cookie' in request.headers:
try:
cookie = Cookie.SimpleCookie()
cookie.load(str(request.headers['cookie']))
except Cookie.CookieError:
pass
else:
if 'cbtid' in cookie:
cbtid = cookie['cbtid'].value
if cbtid in self.factory._cookies:
self._cbtid = cbtid
log.msg("Cookie already set: %s" % self._cbtid)
# if no cookie is set, create a new one ..
if self._cbtid is None:
self._cbtid = newid()
maxAge = 86400
cbtData = {'created': utcnow(),
'authenticated': None,
'maxAge': maxAge,
'connections': set()}
self.factory._cookies[self._cbtid] = cbtData
# do NOT add the "secure" cookie attribute! "secure" refers to the
# scheme of the Web page that triggered the WS, not WS itself!!
##
headers['Set-Cookie'] = 'cbtid=%s;max-age=%d' % (self._cbtid, maxAge)
log.msg("Setting new cookie: %s" % self._cbtid)
# add this WebSocket connection to the set of connections
# associated with the same cookie
self.factory._cookies[self._cbtid]['connections'].add(self)
self._authenticated = self.factory._cookies[self._cbtid]['authenticated']
# accept the WebSocket connection, speaking subprotocol `protocol`
# and setting HTTP headers `headers`
return (protocol, headers)
示例5: onConnect
def onConnect(self, request):
if self.factory.debug_traffic:
from twisted.internet import reactor
def print_traffic():
print("Traffic {}: {} / {} in / out bytes - {} / {} in / out msgs".format(self.peer,
self.trafficStats.incomingOctetsWireLevel,
self.trafficStats.outgoingOctetsWireLevel,
self.trafficStats.incomingWebSocketMessages,
self.trafficStats.outgoingWebSocketMessages))
reactor.callLater(1, print_traffic)
print_traffic()
# if WebSocket client did not set WS subprotocol, assume "wamp.2.json"
#
self.STRICT_PROTOCOL_NEGOTIATION = self.factory._requireWebSocketSubprotocol
# handle WebSocket opening handshake
#
protocol, headers = WampWebSocketServerProtocol.onConnect(self, request)
try:
self._origin = request.origin
# transport authentication
#
self._authid = None
self._authrole = None
self._authmethod = None
# cookie tracking
#
self._cbtid = None
if self.factory._cookiestore:
self._cbtid = self.factory._cookiestore.parse(request.headers)
# if no cookie is set, create a new one ..
if self._cbtid is None:
self._cbtid, headers['Set-Cookie'] = self.factory._cookiestore.create()
if self.debug:
log.msg("Setting new cookie: %s" % headers['Set-Cookie'])
else:
if self.debug:
log.msg("Cookie already set")
# add this WebSocket connection to the set of connections
# associated with the same cookie
self.factory._cookiestore.addProto(self._cbtid, self)
if self.debug:
log.msg("Cookie tracking enabled on WebSocket connection {}".format(self))
# if cookie-based authentication is enabled, set auth info from cookie store
#
if 'auth' in self.factory._config and 'cookie' in self.factory._config['auth']:
self._authid, self._authrole, self._authmethod = self.factory._cookiestore.getAuth(self._cbtid)
if self.debug:
log.msg("Authenticated client via cookie", self._authid, self._authrole, self._authmethod)
else:
if self.debug:
log.msg("Cookie-based authentication disabled")
else:
if self.debug:
log.msg("Cookie tracking disabled on WebSocket connection {}".format(self))
# remember transport level info for later forwarding in
# WAMP meta event "wamp.session.on_join"
#
self._transport_info = {
'type': 'websocket',
'protocol': protocol,
'peer': self.peer,
'http_headers_received': request.headers,
'http_headers_sent': headers
}
# accept the WebSocket connection, speaking subprotocol `protocol`
# and setting HTTP headers `headers`
#
return (protocol, headers)
except Exception:
traceback.print_exc()
示例6: onConnect
def onConnect(self, request):
if self.factory.debug_traffic:
from twisted.internet import reactor
def print_traffic():
print("Traffic {}: {} / {} in / out bytes - {} / {} in / out msgs".format(self.peer,
self.trafficStats.incomingOctetsWireLevel,
self.trafficStats.outgoingOctetsWireLevel,
self.trafficStats.incomingWebSocketMessages,
self.trafficStats.outgoingWebSocketMessages))
reactor.callLater(1, print_traffic)
print_traffic()
protocol, headers = WampWebSocketServerProtocol.onConnect(self, request)
try:
self._origin = request.origin
## transport authentication
##
self._authid = None
self._authrole = None
self._authmethod = None
## cookie tracking
##
self._cbtid = None
if self.factory._cookiestore:
self._cbtid = self.factory._cookiestore.parse(request.headers)
## if no cookie is set, create a new one ..
if self._cbtid is None:
self._cbtid, headers['Set-Cookie'] = self.factory._cookiestore.create()
if self.debug:
log.msg("Setting new cookie: %s" % headers['Set-Cookie'])
else:
if self.debug:
log.msg("Cookie already set")
## add this WebSocket connection to the set of connections
## associated with the same cookie
self.factory._cookiestore.addProto(self._cbtid, self)
if self.debug:
log.msg("Cookie tracking enabled on WebSocket connection {}".format(self))
## if cookie-based authentication is enabled, set auth info from cookie store
##
if 'auth' in self.factory._config and 'cookie' in self.factory._config['auth']:
self._authid, self._authrole, self._authmethod = self.factory._cookiestore.getAuth(self._cbtid)
if self.debug:
log.msg("Authenticated client via cookie", self._authid, self._authrole, self._authmethod)
else:
if self.debug:
log.msg("Cookie-based authentication disabled")
else:
if self.debug:
log.msg("Cookie tracking disabled on WebSocket connection {}".format(self))
## accept the WebSocket connection, speaking subprotocol `protocol`
## and setting HTTP headers `headers`
return (protocol, headers)
except Exception as e:
traceback.print_exc()
示例7: onConnect
def onConnect(self, request):
protocol, headers = WampWebSocketServerProtocol.onConnect(self, request)
self._origin = request.origin
## transport authentication
##
self._authid = None
self._authrole = None
self._authmethod = None
## cookie tracking
##
self._cbtid = None
if 'cookie' in self.factory._config:
cookie_config = self.factory._config['cookie']
cookie_id_field = cookie_config.get('name', 'cbtid')
cookie_id_field_length = int(cookie_config.get('length', 24))
## see if there already is a cookie set ..
if request.headers.has_key('cookie'):
try:
cookie = Cookie.SimpleCookie()
cookie.load(str(request.headers['cookie']))
except Cookie.CookieError:
pass
else:
if cookie.has_key(cookie_id_field):
cbtid = cookie[cookie_id_field].value
if self.factory._cookies.has_key(cbtid):
self._cbtid = cbtid
log.msg("Cookie already set: %s" % self._cbtid)
## if no cookie is set, create a new one ..
if self._cbtid is None:
self._cbtid = util.newid(cookie_id_field_length)
## http://tools.ietf.org/html/rfc6265#page-20
## 0: delete cookie
## -1: preserve cookie until browser is closed
max_age = cookie_config.get('max_age', 86400 * 30 * 12)
cbtData = {'created': util.utcnow(),
'authid': None,
'authrole': None,
'authmethod': None,
'max_age': max_age,
'connections': set()}
self.factory._cookies[self._cbtid] = cbtData
## do NOT add the "secure" cookie attribute! "secure" refers to the
## scheme of the Web page that triggered the WS, not WS itself!!
##
headers['Set-Cookie'] = '%s=%s;max-age=%d' % (cookie_id_field, self._cbtid, max_age)
log.msg("Setting new cookie: %s" % headers['Set-Cookie'])
## add this WebSocket connection to the set of connections
## associated with the same cookie
self.factory._cookies[self._cbtid]['connections'].add(self)
if self.debug:
log.msg("Cookie tracking enabled on WebSocket connection {}".format(self))
## if cookie-based authentication is enabled, set auth info from cookie store
##
if 'auth' in self.factory._config and 'cookie' in self.factory._config['auth']:
self._authid = self.factory._cookies[self._cbtid]['authid']
self._authrole = self.factory._cookies[self._cbtid]['authrole']
self._authmethod = "cookie.{}".format(self.factory._cookies[self._cbtid]['authmethod'])
else:
if self.debug:
log.msg("Cookie tracking disabled on WebSocket connection {}".format(self))
## accept the WebSocket connection, speaking subprotocol `protocol`
## and setting HTTP headers `headers`
return (protocol, headers)
示例8: connectionLost
def connectionLost(self, reason):
WampWebSocketServerProtocol.connectionLost(self, reason)
self.factory.connectionLost(reason)
示例9: connectionMade
def connectionMade(self):
WampWebSocketServerProtocol.connectionMade(self)
self.factory.connectionMade()