1

A 创建了一个非常简单的扭曲应用程序,它启动 TCP 协议并回显您在 STDIN 中键入的内容。

我现在正在尝试创建一个twistd能够以这种方式运行我的应用程序的插件:echo start或者twistd -n echo

当运行twistd -n echo一切按预期工作时,使用echo start命令时出现错误:/home/vagrant/.env/bld/bin/echo: Unknown command: echo

这是我的代码:

回声/插件.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))

回声/点击.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

安装程序.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.

查看输出时有一件事很奇怪:twistd 没有echo注册子命令。

如果我跑步,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 install而不是python setup.py develop,后一个命令有效,但我不想在生产中运行它


编辑

好的,在搜索了为什么axiomatic start有效而不是我的原因后echo start,我通过从安装中删除所有不需要的代码以及我发现的找到了原因(我不声称这是解决方案,我很想听听@glyph 对此的回答)

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.extendTwisted 插件的,在命令行上会发生同样的事情。tapnametwistd

希望很清楚为什么会这样_plgn_echo

我还必须赞扬您添加这些前缀以清楚地尝试清楚地了解代码的每个区域中特别是引用了哪个名称。假设这个答案对您有意义,那么您对这里的代码的理解将比您只是卡echo在任何地方的情况要好得多,即使它一开始碰巧起作用 :-)。

于 2016-03-24T00:45:48.960 回答