1

A は、TCP プロトコルを開始し、STDIN に入力した内容をエコー出力する、完全に単純なねじれたアプリケーションを作成しました。

私は今twistd、この方法でアプリケーションを実行できるプラグインを作成しようとしています:echo startまたはtwistd -n echo

twistd -n echoすべてを実行すると、echo startコマンドを使用するとエラーが発生します。/home/vagrant/.env/bld/bin/echo: Unknown command: echo

これが私のコードです:

エコー/plugins.py

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

from twisted.application import internet
from twisted.internet import endpoints
from twisted.internet.protocol import Factory
from twisted.python import usage

from echo.protocol import EchoProtocol


class Options(usage.Options):
    optParameters = [['port', 'p', 1234, 'Service port.']]


def makeService(options):
    from twisted.internet import reactor

    f = Factory()
    f.protocol = EchoProtocol

    ep = endpoints.TCP4ServerEndpoint(reactor, int(options['port']))
    return internet.StreamServerEndpointService(ep, f)

エコー/プロトコル.py

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

from twisted.internet.protocol import Protocol


class EchoProtocol(Protocol):
    def dataReceived(self, data):
        self.transport.write('You entered: {data}'.format(data=data))

エコー/tap.py

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

import sys

from twisted.python import usage
from twisted.scripts import twistd


class Start(twistd.ServerOptions):
    def parseOptions(self, args):
        sys.argv[1:] = self.getArguments(args)
        print('Starting echo service...')
        twistd.run()

    def getArguments(self, args):
        args.extend(['--pidfile', self.parent.pid])
        args.extend(['_bld_echo'])
        return args


class Options(usage.Options):
    pid = '/tmp/echo.pid'
    subCommands = [['start', None, Start, 'Launch echo service.']]


def main(argv=None):
    o = Options()
    try:
        o.parseOptions(argv)
    except usage.UsageError, e:
        raise SystemExit(str(e))

ツイスト/プラグイン/echo_plugin.py

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

from twisted.application.service import ServiceMaker

Finger = ServiceMaker(
    'EchoServiceMaker',  # name
    'echo.plugins',  # module
    'Description blah-blah.',  # description
    '_plgn_echo')  # tapname

setup.py

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

from setuptools import find_packages
from setuptools import setup

setup(
    name='Echo',
    version='0.0.1',
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            '_ep_echo=echo.tap:main',
        ],
    },
    install_requires=[
        'Twisted==16.0.0',
    ],
    include_package_data=True,
    zip_safe=False,)

ここで私のvirtualenvセットアップ:

(bld)vagrant@/code/echo $ pip list
Echo (0.0.1)
pip (1.4.1)
setuptools (20.3.1)
Twisted (16.0.0)
wsgiref (0.1.2)
zope.interface (4.1.3)

コマンドの前に_ep__bld_を付けました。なぜなら、twistdを介してプログラムを呼び出したとき、または entry_point を直接呼び出したときにどちらが呼び出されたのかわからなかったからです。

実行すると、次のよう_ep_echo startになります。

[twistd -help output...]

twistd reads a twisted.application.service.Application out of a file and runs
it.
Commands:
    conch            A Conch SSH service.
    dns              A domain name server.
    ftp              An FTP server.
    inetd            An inetd(8) replacement.
    mail             An email service
    manhole          An interactive remote debugger service accessible via
                     telnet and ssh and providing syntax coloring and basic line
                     editing functionality.
    manhole-old      An interactive remote debugger service.
    news             A news server.
    portforward      A simple port-forwarder.
    procmon          A process watchdog / supervisor
    socks            A SOCKSv4 proxy service.
    telnet           A simple, telnet-based remote debugging service.
    web              A general-purpose web server which can serve from a
                     filesystem or application resource.
    words            A modern words server
    xmpp-router      An XMPP Router server

/home/vagrant/.env/bld/bin/_ep_echo: Unknown command: _bld_echo

に置き換え_bld_echoても同様_ep_echoです。

echo出力を見ると、奇妙なことが 1 つあります。それは、twistd にサブコマンドが登録されていないことです。

実行すると、次のようtwistd --helpになります。

twistd reads a twisted.application.service.Application out of a file and runs
it.
Commands:
    _plgn_echo       Description blah-blah.
    conch            A Conch SSH service.
    dns              A domain name server.
    ftp              An FTP server.
    inetd            An inetd(8) replacement.
    mail             An email service
    manhole          An interactive remote debugger service accessible via
                     telnet and ssh and providing syntax coloring and basic line
                     editing functionality.
    manhole-old      An interactive remote debugger service.
    news             A news server.
    portforward      A simple port-forwarder.
    procmon          A process watchdog / supervisor
    socks            A SOCKSv4 proxy service.
    telnet           A simple, telnet-based remote debugging service.
    web              A general-purpose web server which can serve from a
                     filesystem or application resource.
    words            A modern words server
    xmpp-router      An XMPP Router server

echoそして、そこにコマンドが登録されていることがわかります。

これは私を夢中にさせています、ここで何が問題なのかについてのアイデアはありますか??

後者のコマンドは機能しますが、本番環境では実行したくありませんpython setup.py installpython setup.py develop


編集

なぜaxiomatic start私ではなく機能したのかを検索した後echo start、インストールから不要なコードをすべて削除することで理由を見つけました。

AxiomEchoの主な違いは、次の行ですsetup.py

packages=find_packages() + ['twisted.plugins']

+ ['twisted.plugins']への追加はありませんでしたが、packages line動作するようになりましたが、まだこのエラーが発生しています:

Unexpected error while writing cache file
Traceback (most recent call last):
  File "/home/vagrant/.env/bld/lib/python2.7/site-packages/Twisted-16.0.0-py2.7-linux-x86_64.egg/twisted/application/app.py", line 579, in parseOptions
    usage.Options.parseOptions(self, options)
  File "/home/vagrant/.env/bld/lib/python2.7/site-packages/Twisted-16.0.0-py2.7-linux-x86_64.egg/twisted/python/usage.py", line 262, in parseOptions
    for (cmd, short, parser, doc) in self.subCommands:
  File "/home/vagrant/.env/bld/lib/python2.7/site-packages/Twisted-16.0.0-py2.7-linux-x86_64.egg/twisted/application/app.py", line 596, in subCommands
    for plug in sorted(plugins, key=attrgetter('tapname')):
  File "/home/vagrant/.env/bld/lib/python2.7/site-packages/Twisted-16.0.0-py2.7-linux-x86_64.egg/twisted/plugin.py", line 213, in getPlugins
    allDropins = getCache(package)
--- <exception caught here> ---
  File "/home/vagrant/.env/bld/lib/python2.7/site-packages/Twisted-16.0.0-py2.7-linux-x86_64.egg/twisted/plugin.py", line 185, in getCache
    dropinPath.setContent(pickle.dumps(dropinDotCache))
exceptions.AttributeError: 'ZipPath' object has no attribute 'setContent'

プラグインは機能しますが、元のインストール方法が機能しなかった理由を知りたいです...

4

1 に答える 1

1

問題について非常に詳細に説明していただきありがとうございます。わずかな微調整(適切なパッケージにするための__init__.py内部の作成)のみで、正確に再現できました。echo/

まず、ここに修正があります:

diff --git a/echo/tap.py b/echo/tap.py
index d23571f..8e1ea84 100644
--- a/echo/tap.py
+++ b/echo/tap.py
@@ -15,7 +15,7 @@ class Start(twistd.ServerOptions):

     def getArguments(self, args):
         args.extend(['--pidfile', self.parent.pid])
-        args.extend(['_bld_echo'])
+        args.extend(['_plgn_echo'])
         return args

これが機能する理由は、このようなコマンドを書いているときにやっていることは、Twistedプラグインの実行をラップしていることです。コマンドラインで行うのと同じことです。args.extendtapnametwistd

うまくいけば、これがなぜなのかは明らかです_plgn_echo

また、これらのプレフィックスを追加して、特にコードの各領域でどの名前が参照されているかを明確に理解しようとすることを称賛する必要があります. echoこの答えがあなたにとって理にかなっていると仮定すると、最初はたまたま機能していたとしても、どこにでも行き詰まっていた場合よりも、ここのコードをはるかによく理解できるでしょう:-)。

于 2016-03-24T00:45:48.960 に答える