我如何使用WAMP autobahn python发布和订阅文件

时间:2018-11-14 08:23:43

标签: python wamp wampserver autobahn twisted.internet

这是我的代码,我想在服务器中发布一个文本文件,并且也要由客户端订阅同一文件,在这里我写了两个程序,一个是服务器,另一个是客户端,但是我不知道如何发布文件与wamp autobahn python配合使用,因此任何人都可以指导我如何发布和订阅文件。

服务器程序:

from __future__ import print_function
import requests
from os import environ
import os
import argparse
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
requests.post("http://wamp.sice.in:8080/notify",
        json={
          'topic': 'service.self.admin',
           'args': [{'type' : 'scrap' }]

        }
    )
class ClientSession(ApplicationSession):
    @inlineCallbacks
    def onJoin(self, details):
        print("session attached")
        self.received = 0
        sub = yield self.subscribe(self.on_event, u'service.self.result')
        print("Service.self.result {}".format(sub.id))

    def on_event(self, i):
        print("Got event: {}".format(i))

    def onDisconnect(self):
        print("disconnected")
        if reactor.running:
            reactor.stop()
if __name__ == '__main__':
    import six

    runner = ApplicationRunner(url= u'ws://wamp.sice.in:8080/ws', realm= u'default')
    runner.run(ClientSession, auto_reconnect=True)

客户端程序:

from __future__ import print_function
from os import environ
import os
import argparse
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
import requests
class ClientSession(ApplicationSession):
    @inlineCallbacks
    def onJoin(self, details):
        print("session attached")
        self.received = 0
        sub = yield self.subscribe(self.on_event, u'service.self.admin')
        print("Service.self.adminh {}".format(sub.id))
    def on_event(self, i):
        print("Got event: {}".format(i))
        requests.post("http://wamp.sice.in:8080/notify",
                json={
                  'topic': 'service.self.result',


                  'args':[{'site1':'data'}]                   
            )
    def onDisconnect(self):
        print("disconnected")
        if reactor.running:
            reactor.stop()
if __name__ == '__main__':
    import six

    runner = ApplicationRunner(url= u'ws://wamp.sice.in:8080/ws', realm= u'default')
    runner.run(ClientSession, auto_reconnect=True)

0 个答案:

没有答案