当检测到文件更改时如何发送消息?扭曲和网络插座

2024-05-04 18:22:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个小的演示程序,在这个演示中,我在我的计算机和本地主机之间连接一个web套接字ws://localhost:8080/ws。我希望web套接字监视计算机上的文件以进行更改。如果有更改,请发送消息。Rest和正在使用高级输出的客户端连接进行监视。在

有没有一个特定的方法可以用来在类上不断检查这个文件的内容?在

编辑

我使用watchdog实现了一个观察者,它检测指定目录中文件的任何事件。但是,我的消息不是用sendFSEvent方法发送的,而且我还意识到,当我连接到web套接字时,我的客户机没有被注册。在

这是我在server.py中的代码

import sys
import os

from watchdog.observers import Observer
from twisted.web.static import File
from twisted.python import log
from twisted.web.server import Site
from twisted.internet import reactor, defer

from autobahn.twisted.websocket import WebSocketServerFactory, \
    WebSocketServerProtocol, listenWS

from MessangerEventHandler import MessangerEventHandler


class WsProtocol(WebSocketServerProtocol):
    def connectionMade(self):
        print("Connection made")
        WebSocketServerProtocol.connectionMade(self)

    def onOpen(self):
        WebSocketServerProtocol.onOpen(self)
        print("WebSocket connection open")

    def onMessage(self, payload, isBinary):
        print("Message was: {}".format(payload))
        self.sendMessage("message received")

    def sendFSEvent(self, json):
        WebSocketProtocol.sendMessage(self, json)
        print('Sent FS event')

    def onClose(self, wasClean, code, reason):
        print("Connection closed: {}".format(reason))
        WebSocketServerProtocol.onClose(self, wasClean, code, reason)


class WsServerFactory(WebSocketServerFactory):
    protocol = WsProtocol

    def __init__(self, url='ws://localhost', port=8080):
        addr = url + ':' + str(port)
        print("Listening on: {}".format(addr))
        WebSocketServerFactory.__init__(self, addr)
        self.clients = []

    def register(self, client):
        if not client in self.clients:
            print("Registered client: {}".format(client))
            self.clients.append(client)

    def unregister(self, client):
        if client in self.clients:
            print("Unregistered client: {}".format(client))
            self.clients.remove(client)
        self._printConnected()

    def _printConnected(self):
        print("Connected clients:[")

    def notify_clients(self, message):
        print("Broadcasting: {}".format(message))
        for c in self.clients:
            c.sendFSEvent(message)
        print("\nSent messages")


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: python server_defer.py <dirs>")
        sys.exit(1)

    log.startLogging(sys.stdout)

    ffactory = WsServerFactory("ws://localhost", 8080)
    ffactory.protocol = WsProtocol
    listenWS(ffactory)

    observers = []
    for arg in sys.argv[1:]:
        dir_path = os.path.abspath(arg)
        if not os.path.exists(dir_path):
            print('{} does not exist.'.format(dir_path))
            sys.exit(1)
        if not os.path.isdir(dir_path):
            print('{} is not a directory.'.format(dir_path))
            sys.exit(1)

        # Check for and handle events
        event_handler = MessangerEventHandler(ffactory, reactor, os.getcwd())

        observer = Observer()
        observer.schedule(event_handler, path=dir_path, recursive=True)
        observer.start()

        observers.append(observer)

    try:
        reactor.run()
    except keyboardInterrupt:
        for obs in observers:
            obs.stop()
        reactor.stop()
        print("\nGoodbye")
        sys.exit(1)

任何帮助都将不胜感激。在

谢谢你

布莱恩


Tags: pathfromimportselfclientwebformatif
1条回答
网友
1楼 · 发布于 2024-05-04 18:22:21

大多数企业发行版都附带inotify,它非常适合监视文件和目录。基本思想是在连接时捕获已连接的web套接字客户端的列表。然后创建一个回调,当您所监视的文件发生更改时将执行该回调。在这个回调中,您可以迭代客户端并向它们发送一条消息,如'file: "blah/blah.txt" has changed'。这有点不靠谱,但是代码片段应该能帮你理清一切。在

from functools import partial
from twisted.internet import inotify
from twisted.python import filepath
# the rest of your imports ...


class SomeServerProtocol(WebSocketServerProtocol):
    def onConnect(self, request):
        self.factory.append(self)       # <== append this client to the list in the factory


def notification_callback(ignored, filepath, mask, ws_clients):
    """
    function that will execute when files are modified
    """
    payload = "event on {0}".format(filepath)
    for client in ws_clients:
        client.sendMessage(
            payload.encode('utf8'),    # <== don't forget to encode the str to bytes before sending!
            isBinary = False)

if __name__ == '__main__':
    root = File(".")
    factory = WebSocketServerFactory(u"ws://127.0.01:8080")
    factory.protocol = SomeServerProtocol
    factory.clients = []        # <== create a container for the clients that connect

    # inotify stuff
    notify = partial(notification_callback, ws_clients=factory.clients)   # <== use functools.partial to pass extra params
    notifier = inotify.INotify()
    notifier.startReading()
    notifier.watch(filepath.FilePath("/some/directory"), callbacks=[notify])

    # the rest of your code ...

相关问题 更多 >