0

I am designing a simple LoRaWAN UDP server based upon Twisted's plugin method. It gives me different results in two PCs.

  1. Aliyun ECS, Ubuntu 12.04(32bit), Python 2.7.3, Twisted 15.0.0
  2. Physical PC, Ubuntu 18.04(64bit), Python 2.7.15, Twisted 17.9.0

I have following plug in trees in my project.

$ tree lorawan_server/
lorawan_server/
├── LoRaWANPktFwd.py
├── run.sh
├── shutdown_twistd.sh
├── start_plugin_twistd.sh
└── twisted
    └── plugins
        └── LoRaWANPktFwd_plugin.py

LoRaWANPktFwd.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 reactor, protocol, defer
from twisted.python import log
from twisted.enterprise import adbapi

class LoRaWANPktFwd(DatagramProtocol):

    def datagramReceived(self, data, addr):
        # A simple echo server
        self.transport.write(data, addr)

def main():
    log.startLogging(sys.stdout)
    reactor.listenUDP(1700, LoRaWANPktFwd())
    reactor.run()

if __name__ == "__main__":
    main()

LoRaWANPktFwd_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 LoRaWANPktFwd import LoRaWANPktFwd

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

class LoRaWANPktFwdServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "LoRaWAN"
    description = "A Raw UDP-based LoRaWAN Packet Forwarder server."
    options = Options

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

serviceMaker = LoRaWANPktFwdServiceMaker()

If I type:

twistd LoRaWAN

then Ubuntu 18.04 will complain following:

/usr/bin/twistd: Unknown command: LoRaWAN

And Ubuntu 12.04 will start to run, with twistd.pid and twistd.log created in same folder.

I have no idea why this happens. Any plugin update in latest twisted? or is it related to systemd or something?

Allan K Liu
  • 493
  • 1
  • 7
  • 23

1 Answers1

0

Twisted 15 and Twisted 17.9 have different behavior with respect to automatically including $PWD in sys.path. Twisted 15 does and Twisted 17.9 does not.

Add the path containing your project to PYTHONPATH (perhaps by creating a virtualenv and using pip to do an editable install of your project into it) and Twisted 17.9 will find the code and therefore also your plugin.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • You mentioned an editable install. Should I run virtualenv and prepare a setup.py to install my lorawan project into the "sys.path", but how can I install the folder of "twisted" for plugin ? More detail is highly appreciated. – Allan K Liu Sep 26 '18 at 05:31
  • https://stackoverflow.com/questions/7275295/how-do-i-write-a-setup-py-for-a-twistd-twisted-plugin-that-works-with-setuptools might help – Jean-Paul Calderone Sep 26 '18 at 14:11