简体   繁体   English

如何使用Twisted守护自定义反应器(txnet)

[英]How to daemonize a custom reactor (txnet) with twistd

I'm using txnet in an application to monitor if some servers are up, it has a web interface so the reactor along with listenICMP for ping requests (is the reactor provided in txnet) also listenTCP for the web server. 我在应用程序中使用txnet监视某些服务器是否启动,它具有Web界面,因此反应器以及用于ping请求的listenICMP(txnet中提供的反应器)也用于Web服务器的listenTCP。 My question is about how to daemonize with twistd. 我的问题是关于如何使用扭曲守护进程。 Twisted.internet.application provide according to the api: Twisted.internet.application根据api提供:

TCPServer, TCPClient, UNIXServer, UNIXClient, SSLServer, SSLClient, UDPServer, UDPClient, UNIXDatagramServer, UNIXDatagramClient, MulticastServer TCPServer,TCPClient,UNIXServer,UNIXClient,SSLServer,SSLClient,UDPServer,UDPClient,UNIXDatagramServer,UNIXDatagramClient,MulticastServer

I'm guessing i have to implement my own service, but i cand find a nice example on doing this, best explanation online probably krondo tutorial daemonology but it has no references on change the reactor. 我猜想我必须实现自己的服务,但是我能找到一个很好的例子,最好的解释可能是krondo教程守护进程,但它没有关于更改反应堆的参考。 Summaring: What is the best choice in order to daemonize something like this?: 总结:守护这种东西的最佳选择是什么?

import json
from txnet.reactor import reactor
from twisted.web import server, resource
from twisted.internet.protocol import DatagramProtocol

result = {}
class PingProtocol(DatagramProtocol):

    ICMP_ECHOREPLY = 0

    def datagramReceived(self, datagram, address):
        ip, port = address
        result[ip] = True

class WebServer(resource.Resource):

    def __init__(self):
        resource.Resource.__init__(self)
        self.putChild('', self)

    def render_GET(self, request):
        str = json.dumps(result)
        result.clear()
        return str

wserver = WebServer()
reactor.listenTCP(8081, server.Site(wserver))
reactor.listenICMP(0, PingProtocol())
reactor.run()

I know that listenUDP probably do the work here, but this is a simplified version, i really need to listenICMP. 我知道listenUDP可能在这里做了工作,但这是一个简化的版本,我真的需要listenICMP。

By writing a twistd plugin you can make use of all features twistd provides, including daemonization. 通过编写twistd 插件 ,你可以使用所有功能twistd提供,包括系统守护进程。 Here is an example . 这是一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM