时间:2018-03-14 06:43:45

标签: udp twisted

我正在尝试添加UDP套接字服务器,侦听NB-IoT设备。我之前的工作经验仅适用于TCP套接字。

EpicGpsUdpServer.py

#!/usr/bin/env python
#coding: utf-8

from __future__ import print_function

import getopt
import os
import sys
import string
import struct
import binascii
import time
import uuid

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import protocol, reactor, defer
from twisted.python import log
from twisted.enterprise import adbapi


class GpsPro(DatagramProtocol):

    def datagramReceived(self, data, addr):
        print("[Peer]{}:{}".format(addr[0], addr[1]))
        print("[Data]{}".format(data))
        if data:
            self.transport.write(data, addr)            

    # def makeConnection(self, data):
    #     pass

class GpsProFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return GpsPro()

def main():
    log.startLogging(sys.stdout)
    reactor.listenUDP(5863, GpsPro())
    #reactor.listenUDP(5863, GpsProFactory())
    reactor.run()

if __name__ == "__main__":
    main()

为了使用twistd来运行它。我添加了

EpicGpsUdpServer_plugin.py

from zope.interface import implements
from twisted.application.service import IServiceMaker
from twisted.application import internet
from twisted.plugin import IPlugin
from twisted.python import usage
#from EpicGlinkUdpServer import GpsFactory
from EpicGpsUdpServer import GpsPro

class Options(usage.Options):
    optParameters = [["port","p", 5863, "The port number to listen on."]]

class GpsServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "Gps"
    description = "A UDP-based GPS server."
    options = Options

    def makeService(self, options):
        #return internet.UDPServer(int(options["port"]), GpsFactory)
        return internet.UDPServer(int(options["port"]), GpsPro)

serviceMaker = GpsServiceMaker()    

如果我运行" twistd --help",Gps将被列为子命令。如果我运行"扭曲Gps",那么它会抛出错误,如:

2018-03-14 14:16:41+0800 Log opened.
2018-03-14 14:16:41+0800 twistd 15.0.0 (/usr/bin/python 2.7.3) starting up.
2018-03-14 14:16:41+0800 reactor class: twisted.internet.epollreactor.EPollReactor.
2018-03-14 14:16:41+0800 Traceback (most recent call last):
2018-03-14 14:16:41+0800   File "/usr/local/bin/twistd", line 5, in <module>
2018-03-14 14:16:41+0800     pkg_resources.run_script('Twisted==15.0.0', 'twistd')
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 748, in run_script
2018-03-14 14:16:41+0800     self.require(requires)[0].run_script(script_name, ns)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1517, in run_script
2018-03-14 14:16:41+0800     exec(code, namespace, namespace)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/EGG-INFO/scripts/twistd", line 14, in <module>
2018-03-14 14:16:41+0800     run()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/scripts/twistd.py", line 27, in run
2018-03-14 14:16:41+0800     app.run(runApp, ServerOptions)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/application/app.py", line 645, in run
2018-03-14 14:16:41+0800     runApp(config)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/scripts/twistd.py", line 23, in runApp
2018-03-14 14:16:41+0800     _SomeApplicationRunner(config).run()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/application/app.py", line 383, in run
2018-03-14 14:16:41+0800     self.postApplication()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/scripts/_twistd_unix.py", line 193, in postApplication
2018-03-14 14:16:41+0800     self.startApplication(self.application)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/scripts/_twistd_unix.py", line 381, in startApplication
2018-03-14 14:16:41+0800     service.IService(application).privilegedStartService()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/application/service.py", line 275, in privilegedStartService
2018-03-14 14:16:41+0800     service.privilegedStartService()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/application/internet.py", line 105, in privilegedStartService
2018-03-14 14:16:41+0800     self._port = self._getPort()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/application/internet.py", line 133, in _getPort
2018-03-14 14:16:41+0800     'listen%s' % (self.method,))(*self.args, **self.kwargs)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/internet/posixbase.py", line 373, in listenUDP
2018-03-14 14:16:41+0800     p = udp.Port(port, protocol, interface, maxPacketSize, self)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/internet/udp.py", line 114, in __init__
2018-03-14 14:16:41+0800     self.setLogStr()
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/internet/udp.py", line 364, in setLogStr
2018-03-14 14:16:41+0800     logPrefix = self._getLogPrefix(self.protocol)
2018-03-14 14:16:41+0800   File "/usr/local/lib/python2.7/dist-packages/Twisted-15.0.0-py2.7-linux-i686.egg/twisted/internet/abstract.py", line 143, in _getLogPrefix
2018-03-14 14:16:41+0800     return applicationObject.__class__.__name__
2018-03-14 14:16:41+0800 AttributeError: class GpsPro has no attribute '__class__'

问题

很抱歉,UDP服务器带来了很多问题,我想说清楚。

  1. 我的协议的 class 属性是什么?
  2. UDP服务器协议是否支持工厂?由于TCP服务器支持工厂,我试过,但我有一些额外的错误。在DNS服务器中,扭曲的文档显示它有工厂,但我找不到原始UDP套接字的参考代码。
  3. 虽然UDP服务器不是面向连接的,但服务器可以在1分钟内在NBIoT设备的短时间内用peer ip:port回写,大约20秒。但由于没有工厂,代码无处可以保留上下文,就像TCP连接中的对应部分一样。
  4. 任何简单的代码/项目都会有所帮助。感谢。

1 个答案:

答案 0 :(得分:0)

  

我的协议的__class__属性是什么?

如果您有课程Foo,则Foo().__class__Foo。发生此错误的原因是您将GpsPro而不是GpsPro()传递给internet.UDPServer

  

UDP服务器协议是否支持工厂?由于TCP服务器支持工厂,我试过,但我有一些额外的错误。在DNS服务器中,扭曲的文档显示它有工厂,但我找不到原始UDP套接字的参考代码。

Twisted的UDP不适用于工厂。您只需使用协议实例。

  

虽然UDP服务器不是面向连接的,但服务器可以在1分钟内在NBIoT设备的短时间内用peer ip:port回写,大约20秒。但由于没有工厂,代码无处可以保留上下文,就像TCP连接中的对应部分一样。

您可以在协议实例上为此创建必要的状态。