【问题标题】:Import error No module named constant_time while accessing server访问服务器时导入错误 No module named constant_time
【发布时间】:2017-04-06 07:24:14
【问题描述】:

这是Import Modules in Nifi ExecuteScript的跟进

我是 python 和 nifi 的新手。我正在尝试在 ExecuteScript 处理器中执行我的 python 脚本。

我想访问服务器。所以我使用了 paramiko 客户端。但是当我运行处理器时,它在 session.write() 行显示“导入错误没有名为 constant_time 的模块”。虽然我在“/usr/local/lib/python2.7/dist-packages/”下有这个 constant_time.py

我在 sys.path 中也有路径“/usr/local/lib/python2.7/dist-packages/”。我还在“模块目录”属性中给出了这个路径。

这是我的代码:

import json, pysftp, paramiko
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback

class ModJSON(StreamCallback):
  def __init__(self):
    pass
  def process(self, inputStream, outputStream):

    text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    inputText = text.rstrip('\r\n')
    json_content = json.loads(inputText)
    body = ''
    try:
       body = json_content['id']['body']
       body_encoded = body.encode('utf-8')
    except (KeyError,TypeError,ValueError):
       body_encoded = ''

    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.load_system_host_keys()
    ssh_client.connect('server', username='xxx', password='xxxx')

    sftp_client = ssh_client.open_sftp()
    text_file = sftp_client.open ('/doc/body.txt', 'w')   
    text_file.write("%s"%body_encoded)
    text_file.close()
    outputStream.write(bytearray(json.dumps(body, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
   flowFile = session.write(flowFile, ModJSON())
   flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
   session.transfer(flowFile, REL_SUCCESS)

任何帮助将不胜感激。

【问题讨论】:

标签: python-2.7 paramiko apache-nifi constant-time python-cryptography


【解决方案1】:

正如 Matt Burgess 在 this answer 中所解释的,ExecuteScript 处理器使用 Jython,而不是 Python,因此编译的模块不可用。目前正在讨论如何/是否添加此功能。

【讨论】:

  • 是的。在那次讨论中,解释了如何导入模块。据说在 ExecuteScript Processor 的“Module Directory”属性中添加了 lib 路径。我也在“模块目录”中添加了这个。但它仍然显示错误。所以你的意思是我永远不能导入这样的模块?
  • 我相信目前就是这种情况——正如马特所写的那样:“根据this SO post,paramiko 使用具有本机库的 Crypto,因此不能在 Jython 中使用(请参阅this post 的底部)我对此发表评论)。”
  • 哦!谢谢安迪。那么,是否有任何其他模块可以导入以访问服务器?或者在 ExecuteScript 中访问和使用服务器的其他方式是什么?
  • 您有几个选择。您可以尝试查找不包含或不依赖已编译模块的 Python 模块,您可以在 Jython 中使用该模块。我不知道是否存在。您还可以为ExecuteScript 处理器切换到 Groovy(我 100% 确定)或 Ruby(70% 确定),因为它们都允许包含模块。我无法预测 NiFi Jython 是否/何时会接受已编译的模块。
  • 谢谢安迪让我试试。
猜你喜欢
  • 2016-10-31
  • 1970-01-01
  • 2022-08-23
  • 1970-01-01
  • 2012-12-28
  • 2022-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多