【问题标题】:How to avoid ImportError in twisted tac file?如何避免扭曲的 tac 文件中的 ImportError?
【发布时间】:2017-10-17 07:19:44
【问题描述】:

我有一个带有代码的twisted tac 文件(twisted_service.py):

from twisted.application import service
# application.py file in the same dir
from .application import setup_reactor


class WebsocketService(service.Service):

    def startService(self):
        service.Service.startService(self)
        setup_reactor()

application = service.Application("ws")

ws_service = WebsocketService()
ws_service.setServiceParent(application)

这里是 application.py 文件,它设置了反应器:

# -*- coding: utf-8 -*-
from twisted.web.server import Site
from twisted.web.static import Data
from twisted.internet import reactor, defer
from autobahn.twisted.resource import WebSocketResource
from autobahn.twisted.websocket import WebSocketServerFactory
from txsni.snimap import SNIMap
from txsni.maputils import Cache
from txsni.snimap import HostDirectoryMap
from twisted.python.filepath import FilePath
from tools.database.async import pg_conn
from tools.database import makedsn
from tools.config import main_db
from tools.modules.external import flask_setup


import tools.config as config
import websockethandlers as wsh
from pytrapd import TrapsListener


PROTOCOLMAP = {
    'portcounters': wsh.PortCounters,
    'eqcounters': wsh.EquipmentCounters,
    'settings': wsh.Settings,
    'refresh': wsh.Refresher,
    'montraps': wsh.TrapsMonitoring,
    'fdbs': wsh.FdbParser,
    'portstate': wsh.PortState,
    'cable': wsh.CableDiagnostic,
    'eqcable': wsh.EquipmentCableDiagnostic,
    'igmp': wsh.Igmp,
    'ipmac': wsh.IpMac,
    'lldp': wsh.LLDPParser,
    'alias': wsh.AliasSetup,
    'ping': wsh.Ping
}


@defer.inlineCallbacks
def setup_reactor():
    flask_setup()
    yield pg_conn.connect(makedsn(main_db))
    root = Data("", "text/plain")
    for key in PROTOCOLMAP:
        factory = WebSocketServerFactory("wss://localhost:%s" % config.ws_port)
        factory.protocol = PROTOCOLMAP[key]
        resource = WebSocketResource(factory)
        root.putChild(key, resource)
    site = Site(root)
    context_factory = SNIMap(
        Cache(HostDirectoryMap(FilePath(config.certificates_directory)))
    )
    reactor.listenSSL(config.ws_port, site, context_factory)
    traps_listener = TrapsListener()
    traps_listener.listen_traps(config.trap_ip)
    traps_listener.listen_messages(config.fifo_file)

if __name__ == '__main__':
    setup_reactor()
    import sys

    from twisted.python import log
    log.startLogging(sys.stdout)
    reactor.run()

我使用twistd -noy twisted_service.py 命令来运行twisted 服务。它一直适用于扭曲的 16.3.2 版本。升级到任何下一个版本后,我收到错误:

Unhandled Error
Traceback (most recent call last):
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 662, in run
    runApp(config)
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/scripts/twistd.py", line 25, in runApp
    _SomeApplicationRunner(config).run()
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 380, in run
    self.application = self.createOrGetApplication()
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 445, in createOrGetApplication
    application = getApplication(self.config, passphrase)
--- <exception caught here> ---
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/app.py", line 456, in getApplication
    application = service.loadApplication(filename, style, passphrase)
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/application/service.py", line 412, in loadApplication
    application = sob.loadValueFromFile(filename, 'application')
  File "/home/kalombo/.virtualenvs/dev/local/lib/python2.7/site-packages/twisted/persisted/sob.py", line 177, in loadValueFromFile
    eval(codeObj, d, d)
  File "twisted_service.py", line 3, in <module>
    from .application import setup_reactor
exceptions.ImportError: No module named application

我应该如何正确运行twisted或import模块?

【问题讨论】:

  • 这段代码来自哪里? from .application 看起来可能是错误的,但我不知道你的代码。
  • 我已经更新了问题

标签: python twisted


【解决方案1】:

我找到的答案几乎是你的。如下:

import os
import sys

sys.path = [os.path.join(os.getcwd(), '.'), ] + sys.path

只需将当前工作目录添加到 sys.path。

但是我还没有找到更好的方法....我认为这不是很好。

【讨论】:

    【解决方案2】:

    我在这里找到了答案http://twistedmatrix.com/pipermail/twisted-python/2016-September/030783.html

    这是 Twisted 16.4.0 中的一项新功能。在以前的版本中,twistd 脚本自动将工作目录添加到系统路径,从 16.4.0 版本开始,我必须手动添加它。可以在 twisted_service.py 文件中添加类似这样的内容:

    import os
    import sys
    TWISTED_DIR = os.path.dirname(os.path.abspath(__file__))
    sys.path.append(TWISTED_DIR)
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 2016-01-20
      • 2020-02-02
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多