tahoe-lafs/ws_client.py

87 lines
2.1 KiB
Python
Raw Normal View History

2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
import sys
2019-03-21 08:26:38 +00:00
import json
2019-03-21 00:14:56 +00:00
2019-03-21 17:36:09 +00:00
from twisted.internet.error import ConnectError
2019-03-21 07:37:47 +00:00
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks, Deferred
2019-03-21 17:36:09 +00:00
from twisted.internet.endpoints import HostnameEndpoint
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
from autobahn.twisted.websocket import (
WebSocketClientProtocol,
WebSocketClientFactory,
)
2019-03-21 00:14:56 +00:00
2019-03-21 08:26:38 +00:00
from allmydata.client import read_config
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
class TahoeLogProtocol(WebSocketClientProtocol):
"""
"""
2019-03-21 00:14:56 +00:00
def onOpen(self):
2019-03-21 08:26:38 +00:00
self.factory.on_open.callback(self)
2019-03-21 00:14:56 +00:00
def onMessage(self, payload, isBinary):
2019-03-21 07:37:47 +00:00
if False:
log_data = json.loads(payload.decode('utf8'))
print("eliot message:")
for k, v in log_data.items():
print(" {}: {}".format(k, v))
2019-03-21 00:14:56 +00:00
else:
2019-03-21 07:37:47 +00:00
print(payload)
sys.stdout.flush()
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
def onClose(self, *args):
2019-03-21 08:26:38 +00:00
if not self.factory.on_open.called:
self.factory.on_open.errback(
2019-03-21 17:35:54 +00:00
RuntimeError("Failed: {}".format(args))
2019-03-21 08:26:38 +00:00
)
self.factory.on_close.callback(self)
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
@inlineCallbacks
def main(reactor):
2019-03-21 00:14:56 +00:00
2019-03-21 08:26:38 +00:00
from twisted.python import log
log.startLogging(sys.stdout)
tahoe_dir = "testgrid/alice"
cfg = read_config(tahoe_dir, "portnum")
token = cfg.get_private_config("api_auth_token").strip()
webport = cfg.get_config("node", "web.port")
if webport.startswith("tcp:"):
port = webport.split(':')[1]
else:
port = webport
2019-03-21 00:14:56 +00:00
factory = WebSocketClientFactory(
2019-03-21 19:00:02 +00:00
url=u"ws://127.0.0.1:{}/private/logs/v1".format(port),
2019-03-21 00:14:56 +00:00
headers={
2019-03-21 07:37:47 +00:00
"Authorization": "tahoe-lafs {}".format(token),
2019-03-21 00:14:56 +00:00
}
)
2019-03-21 08:26:38 +00:00
factory.on_open = Deferred()
factory.on_close = Deferred()
2019-03-21 07:37:47 +00:00
factory.protocol = TahoeLogProtocol
2019-03-21 08:26:38 +00:00
2019-03-21 17:36:09 +00:00
endpoint = HostnameEndpoint(reactor, "127.0.0.1", int(port))
try:
port = yield endpoint.connect(factory)
except ConnectError as e:
print("Connection failed: {}".format(e))
return
2019-03-21 08:26:38 +00:00
print("port: {}".format(port))
yield factory.on_open
print("opened")
yield factory.on_close
print("closed")
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
if __name__ == '__main__':
react(main)