无法在JMeter中导入python模块

2024-09-28 21:39:32 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试使用JMeter测试sftp服务器的性能,同时用python代码将文件上载到服务器

上载文件的我的代码:

import socket
import os
import time
import threading
import paramiko

#-------
#defining the variables: host, port, user, password, serverpath, localpath, uploadDataAmount and sleepTime
#here I just define the variables it is not related to the problem. 
#I didn't add it because it contains ip and password of the server. 
#-------

transport = paramiko.Transport((host, port))
transport.connect(None, user, password)
sftp = paramiko.SFTPClient.from_transport(transport)


fileServer = sftp.open(serverpath, 'ab')
fileClient = open(localpath, 'rb')
try:
    data = fileClient.read(uploadDataAmount)
    while(data != b''):
        fileServer.write(data)
        size = str(fileServer._get_size())
        print("file size in bytes before going to sleep: "+size)
        time.sleep(sleepTime)
        print("waking up ")
        data = fileClient.read(uploadDataAmount)
finally:
    fileClient.close()
    fileServer.close()

if sftp:
    sftp.close()
if transport:
    transport.close()

我在visualstudio中测试了python代码,效果很好

我是JMeter新手,所以我不确定它是如何工作的,为了使它能够与python代码一起工作,我下载了jython.jar文件,并将其添加到JMeter目录中的lib目录中。但是我的代码使用paramiko包,因此当我尝试运行它时,jmeter返回错误: Response message:javax.script.ScriptException: ImportError: No module named paramiko in <script>

我尝试将paramiko导入更改为:

import sys

sys.path.append(
    "C:\\Users\\YShay\\AppData\\Local\\Programs\\Python\\Python38\\Lib\\site-packages")
from paramiko import Transport, SFTPClient

同样,它在VisualStudio中运行良好

但是我在jmeter中得到了以下错误: Response message:javax.script.ScriptException: ImportError: No module named six in <script>

有人能帮我吗?如何在jmeter中的python代码中导入paramiko


Tags: 文件the代码importparamikoclosedatasize
1条回答
网友
1楼 · 发布于 2024-09-28 21:39:32

我的期望是,您将这个paramiko模块安装到Python中,并且需要将其安装到Jython中,以便能够与JMeter的JSR223采样器一起使用

比如:

jython -m pip install paramiko

更多信息:Extending Jython with pypi modules


一般来说,您的方法不是最好的,starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting,所以考虑切换到Groovy,您可以通过^ {A3}库获得SSH/SFTP连接。

如果您只需要使用SFTP协议将文件上载到服务器,您可以考虑使用SSH SFTP Sampler,这样您甚至不必编写一行代码,并将“异类”Python引入纯java环境中。查看Load Testing FTP and SFTP Servers Using JMeter文章了解全面信息

相关问题 更多 >