python如何执行shell命令

  Jmeter是公司做接口测试的一个开源项目,今天在研究如何用python写个脚本去执行这个jmeter脚本,Jmeter有命令行模式和界面模式,设置好了环境之后,我用了最简单的一条命令做了测试:

jmeter -n -t <testplan filename> -l <listener filename>

  比如:jmeter -n -t  ..../文件.jmx  -l  result.txt  这里忽略jmx文件的编写,这些是测试写好的,我只要调用命令去执行就好了,后期写个定时任务来完成。

   由于是用pycharm,配置好文件路径之后,我直接调用了os.system()来执行,不料却报错,后来用后来用subprocess.Popen也出现了错误,都提示jmeter命令找不到:

  

import os

import sys

currpath = os.path.dirname(os.path.realpath(__file__))  # 当前文件目录
sys.path.insert(0, currpath)
# # print(currpath)
JmxTemlFileName = r‘/Users/admin/Documents/jmeter/stu_tea_test.jmx‘  # 要执行的文件
#
JMETER_Home = r‘‘‘"/Users/admin/Downloads/apache-jmeter-5.1.1/bin/jmeter.bat"‘‘‘  # jmeter执行文件

Jmeter_Out = currpath + ‘/result.txt‘

def runCmd(cmd):
    print(f"command={cmd}")
    os.system(cmd)   ########### 下面使用subprocess.Popen来调用shell############
    # res = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    # print(‘sys‘,sys.path)
    # stdoutinfo,stderrinfo = res.communicate()
    # print(f"stderrinfo={stderrinfo}")
    # print(f"stdoutinfo={stdoutinfo}")
    # print("returncode={0}".format(res.returncode))
exec_str = f"jmeter -n -t {JmxTemlFileName} -l {Jmeter_Out}"

if __name__ == ‘__main__‘:    runCmd(exec_str)
 
/usr/local/bin/python3.6 /Users/admin/PycharmProjects/untitled/t/jmeter_test/jmt_test.py
command=jmeter -n -t /Users/admin/Documents/jmeter/stu_tea_test.jmx -l /Users/admin/PycharmProjects/untitled/t/jmeter_test/result.txt
sh: jmeter: command not found

  明明我的环境都配置好了,为什么在终端运行的好好地,在pycharm却无法运行呢?在网上搜索了一下,发现应该还是环境问题,pycharm的环境和我们系统的环境还是不一样的

  于是我使用终端打开python3,运行了一下之前的文件,发现ok。其实也可以在pycharm中配置jmeter的环境变量也可以运行起来。

  总结:pycharm的环境变量是独立的,与系统的环境变量没有关系,当我们运行不起来时,要检查一下pycharm的环境变量是否有我们需要的bash命令。

原文地址:https://www.cnblogs.com/jimmyhe/p/10836005.html

时间: 2025-01-09 13:16:06

python如何执行shell命令的相关文章

C++/Php/Python 语言执行shell命令

编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 5 int exec_cmd(std::string cmd, std::string &res){ 6 if (cmd.size() == 0){ //cmd is empty 7 return -1; 8 } 9 10 char

python中执行shell命令

sh是一个比subprocess好的库,能够执行shell命令 1.查看ip: [[email protected] myblog]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.31.253.193 netmask 255.255.240.0 broadcast 172.31.255.255 ether 00:16:3e:01:72:ac txqueuelen 1000 (Etherne

python之执行shell命令

[[email protected] ~]# python Python 2.7.5 (default, Sep 15 2016, 22:37:39)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> impor

python-执行shell命令的模块使用

最近,学习别人的blog,总结了下几个知识点,以备不时之需,面试中偶尔被问到:一种结果多种方法.以下为: 获取系统执行命令的返回值和输出结果: 1.os.system: 调用系统命令,完成后退出,显示输出.返回值(一般执行成功就为0). >>> import os >>> os.system('pwd')     /home/ubuntu     0 2.os.popen: 返回的是 file read 的对象,对其再进行read() 的操作可以看到执行的输出 >

使用sh库执行shell命令

python中执行shell命令 之前执行shell命令多是通过os.system(shell命令)的方式来执行,比较麻烦. 了解到sh是一个比subprocess好的库,能够执行shell命令 1.查看ip: [[email protected] myblog]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.31.253.193 netmask 255.255.240.0 broadcas

python中执行shell的两种方法总结

这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包均是Python现有的内置模块.需要的朋友可以参考借鉴,下面来一起看看吧. 一.使用python内置commands模块执行shell commands对Python的os.popen()进行了封装,使用SHELL命令字符串作为其参数,返回命令的结果数据以及命令执行的状态: 该命令目前已经废弃,被s

python执行shell命令

python执行shell命令 #!/usr/bin/python2.7 #coding=utf-8 import shlex import datetime import subprocess import time def execute_command(cmdstring, cwd=None, timeout=None, shell=False): # 执行一个SHELL命令 ## 封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr ## 参数:

python 执行shell 命令,自动化添加攻击IP地址到iptables

通过python执行shell命令的方法有4种,在这里介绍一种常用的. os.system. os.popen. commands. subprocess 接下来介绍subprocess的使用 通过python 日志分析,获取到攻击源IP地址,收集写入到mysql数据库中 mysql如下: iptables.py 脚本 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import MySQLdb import subprocess t

Python之路35-subprocess模块和Python3中常用执行shell命令方法

import subprocess #执行命令,返回命令执行状态 , 0 or 非0 retcode = subprocess.call(["free","-m"])    #返回值可判断执行是否正确,命令执行结果直接返回到屏幕 #执行命令,如果命令结果为0,就正常返回,否则抛异常 result = subprocess.check_call(["ls","-l"]) #执行命令,并返回结果,注意是返回结果,不是打印,下例结果