tahoe-lafs/ws_client.py

64 lines
1.5 KiB
Python
Raw Normal View History

2019-03-21 07:37:47 +00:00
from __future__ import print_function
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
import json
import sys
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks, Deferred
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 07:37:47 +00:00
class TahoeLogProtocol(WebSocketClientProtocol):
"""
"""
2019-03-21 00:14:56 +00:00
def onOpen(self):
2019-03-21 07:37:47 +00:00
pass#print("connected")
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):
print("bye", args)
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 07:37:47 +00:00
with open("testgrid/alice/private/api_auth_token", "r") as f:
#with open("alice/private/api_auth_token", "r") as f:
2019-03-21 00:14:56 +00:00
token = f.read().strip()
factory = WebSocketClientFactory(
2019-03-21 07:37:47 +00:00
url=u"ws://127.0.0.1:8890/logs_v1",
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 07:37:47 +00:00
factory.protocol = TahoeLogProtocol
port = yield reactor.connectTCP("127.0.0.1", 8890, factory)
if False:
print("port {}".format(port))
print(dir(port))
print(port.getDestination())
print(port.transport)
print(dir(port.transport))
print(port.transport.protocol)
# can we like 'listen' for this connection/etc to die?
yield Deferred()
2019-03-21 00:14:56 +00:00
2019-03-21 07:37:47 +00:00
if __name__ == '__main__':
react(main)