本文整理汇总了Python中autobahn.twisted.websocket.WampWebSocketServerProtocol.onConnect方法的典型用法代码示例。如果您正苦于以下问题:Python WampWebSocketServerProtocol.onConnect方法的具体用法?Python WampWebSocketServerProtocol.onConnect怎么用?Python WampWebSocketServerProtocol.onConnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autobahn.twisted.websocket.WampWebSocketServerProtocol
的用法示例。
在下文中一共展示了WampWebSocketServerProtocol.onConnect方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: onConnect
# 需要导入模块: from autobahn.twisted.websocket import WampWebSocketServerProtocol [as 别名]
# 或者: from autobahn.twisted.websocket.WampWebSocketServerProtocol import onConnect [as 别名]
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)
示例2: onConnect
# 需要导入模块: from autobahn.twisted.websocket import WampWebSocketServerProtocol [as 别名]
# 或者: from autobahn.twisted.websocket.WampWebSocketServerProtocol import onConnect [as 别名]
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()
示例3: onConnect
# 需要导入模块: from autobahn.twisted.websocket import WampWebSocketServerProtocol [as 别名]
# 或者: from autobahn.twisted.websocket.WampWebSocketServerProtocol import onConnect [as 别名]
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()
示例4: onConnect
# 需要导入模块: from autobahn.twisted.websocket import WampWebSocketServerProtocol [as 别名]
# 或者: from autobahn.twisted.websocket.WampWebSocketServerProtocol import onConnect [as 别名]
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)