Created
January 9, 2019 19:53
-
-
Save om26er/416a59e786ac976b6915adb158225b9f to your computer and use it in GitHub Desktop.
apt over wamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shlex | |
import subprocess | |
from autobahn.twisted import wamp | |
from autobahn.wamp import types as autobahn_types | |
from twisted.internet.defer import inlineCallbacks, returnValue | |
from twisted.internet import threads | |
class APTSession(wamp.ApplicationSession): | |
def __init__(self, config=None): | |
super().__init__(config) | |
@inlineCallbacks | |
def apt_update(self): | |
def actually_update(): | |
process = subprocess.Popen(shlex.split('apt update'), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
for line in iter(process.stdout.readline, b''): | |
if line: | |
print(line.decode().strip()) | |
self.publish("io.crossbar.apt.progress", line.decode().strip()) | |
res = yield threads.deferToThread(actually_update) | |
returnValue(res) | |
@inlineCallbacks | |
def onJoin(self, details): | |
self.log.info('session joined: {}'.format(details)) | |
yield self.register(self.apt_update, "io.crossbar.apt.update") | |
def onLeave(self, details): | |
self.log.info('session left: {}'.format(details)) | |
self.disconnect() | |
if __name__ == '__main__': | |
session = APTSession(autobahn_types.ComponentConfig('realm1', {})) | |
runner = wamp.ApplicationRunner(u'ws://localhost:8080/ws', u'realm1') | |
runner.run(session, auto_reconnect=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment