Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def connect(self):
factory = WebSocketClientFactory("ws://%s:%s/ws/" % (self.host, self.port), debug=False)
factory.noisy = True
factory.protocol = MdcloudWebsocketClientProtocol
factory.protocol.client = self
self.onc = defer.Deferred()
key_path = os.path.expanduser('~/.mcloud/%s.key' % self.host)
crt_path = os.path.expanduser('~/.mcloud/%s.crt' % self.host)
class NoKeyError(Exception):
pass
try:
if not self.no_ssl and self.host != '127.0.0.1':
if not os.path.exists(key_path):
def run_client(self):
self.factory = WebSocketClientFactory(self.ws_url + '?action=subscribe')
self.factory.protocol = MyClientProtocol
def handler(record):
self.stream.append(record)
self.factory.handler = handler
connectWS(self.factory)
self.factory.reactor.callLater(1, hello)
# start sending messages every second ..
hello()
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
class MyClientFactory(WebSocketClientFactory, ReconnectingClientFactory):
protocol = MyClientProtocol
def clientConnectionFailed(self, connector, reason):
print("Client connection failed .. retrying ..")
self.retry(connector)
def clientConnectionLost(self, connector, reason):
print("Client connection lost .. retrying ..")
self.retry(connector)
if __name__ == '__main__':
import sys
def buildProtocol(self, addr):
proto = websocket.WebSocketClientFactory.buildProtocol(self, addr)
proto.wormhole = self.wormhole
proto.wormhole_open = False
return proto
def __init__(self, *args, **kwargs):
WebSocketClientFactory.__init__(self, *args, **kwargs)
self.factory.user_stream.initialize()
def onMessage(self, payload, isbinary):
# explicit decode for Python 3 compatibility
self.factory.user_stream.on_message(payload.decode('utf8'))
def onClose(self, wasclean, code, reason):
if not wasclean:
self.factory.user_stream.on_error(
Exception('WebSocket closed with error - %s : %s' % (code, reason))
)
else:
self.factory.user_stream.on_disconnect('WebSocket closed cleanly - %s : %s' % (code, reason))
class UserStreamClientFactory(WebSocketClientFactory):
protocol = UserStreamClientProtocol
def __init__(self, user_stream):
super(UserStreamClientFactory, self).__init__(user_stream.user_stream_url)
self.user_stream = user_stream
:param topicUri: URI or CURIE of topic to unsubscribe from.
:type topicUri: str
"""
if type(topicUri) not in [unicode, str]:
raise Exception("invalid type for parameter 'topicUri' - must be string (was %s)" % type(topicUri))
turi = self.prefixes.resolveOrPass(topicUri) ### PFX - keep
if self.subscriptions.has_key(turi):
msg = [WampProtocol.MESSAGE_TYPEID_UNSUBSCRIBE, topicUri]
o = self.factory._serialize(msg)
self.sendMessage(o)
del self.subscriptions[turi]
class WampClientFactory(WebSocketClientFactory, WampFactory):
"""
Twisted client factory for WAMP.
"""
protocol = WampClientProtocol
def __init__(self,
url,
debug = False,
debugCodePaths = False,
debugWamp = False,
debugApp = False,
reactor = None):
self.debugWamp = debugWamp
self.debugApp = debugApp
WebSocketClientFactory.__init__(self,
:param topicUri: URI or CURIE of topic to unsubscribe from.
:type topicUri: str
"""
if type(topicUri) not in [unicode, str]:
raise Exception("invalid type for parameter 'topicUri' - must be string (was %s)" % type(topicUri))
turi = self.prefixes.resolveOrPass(topicUri) ### PFX - keep
if self.subscriptions.has_key(turi):
msg = [WampProtocol.MESSAGE_TYPEID_UNSUBSCRIBE, topicUri]
o = self.factory._serialize(msg)
self.sendMessage(o)
del self.subscriptions[turi]
class WampClientFactory(WebSocketClientFactory, WampFactory):
"""
Twisted client factory for WAMP.
"""
protocol = WampClientProtocol
def __init__(self,
url,
debug = False,
debugCodePaths = False,
debugWamp = False,
debugApp = False,
reactor = None):
self.debugWamp = debugWamp
self.debugApp = debugApp
WebSocketClientFactory.__init__(self,
self._connection.disconnect()
def pre_connect(self):
log.msg('connecting...')
def post_connect(self):
log.msg('connected')
def pre_message(self):
log.msg('receiving message...')
def post_message(self, diffusion_message):
log.msg('received message: %s' % diffusion_message)
class DiffusionFactory(WebSocketClientFactory, ReconnectingClientFactory):
"""
maxDelay = 3600 (seconds)
initialDelay = 1.0 (seconds)
factor = 2.7182818284590451 # (math.e)
jitter = 0.11962656472
Reference: https://github.com/twisted/twisted/blob/trunk/src/twisted/internet/protocol.py#L332
"""
def clientConnectionFailed(self, connector, reason):
self.retry(connector)
def clientConnectionLost(self, connector, reason):
self.retry(connector)
class DiffusionMessage(object):
def addnewnode(host):
ws = "ws://{}:9000".format(host)
#factory = WebSocketClientFactory(u"ws://127.0.0.1:9000")
factory = WebSocketClientFactory(ws)
factory.protocol = MyClientProtocol
reactor.connectTCP(host, 9000, factory)