简体   繁体   English

创建一个python函数以在终端中运行speedtest-cli / ping并将结果输出到日志文件

[英]Create a python function to run speedtest-cli/ping in terminal and output result to a log file

I am learning python and I'm trying to run some terminal command lines using python; 我正在学习python,并尝试使用python运行一些终端命令行; eg: speed test and ping. 例如:速度测试和ping。 I am using functional programming as my method of programming. 我正在使用函数式编程作为我的编程方法。 However, upon reading more and browsing more with functional programming based from docs.python.org 1 . 但是,在使用基于docs.python.org 1的函数式编程来阅读和浏览更多内容时。 I don't think that I am doing it the right way. 我认为我做的方法不正确。

My question is: 我的问题是:
Is it good for a function to not have an argument/parameters and just input the command/s directly inside it? 函数不带参数/参数而只在其中直接输入命令对它有好处吗?
And is it really a good option to use os.system or is there a better module to use? 并且使用os.system确实是一个不错的选择,还是有一个更好的模块可以使用?

Here is the sample of my code. 这是我的代码示例。

#!/usr/bin/python3
# tasks.py

import os

def task_speedtest():
    os.system("speedtest-cli >> /Desktop/logs")

def task_ping():
    os.system("ping www.google.com -c5 >> /Desktop/logs")

task_speedtest()
task_ping()

Regarding your first question, there is nothing wrong with directly executing the commands in the function without using arguments/parameters in your function. 关于第一个问题,直接执行函数中的命令而无需在函数中使用参数/参数并没有错。

You could always add a parameter in your function definition to specify the path for example so that you can call the function and execute the command using different directories: 您可以始终在函数定义中添加参数以指定路径,例如,以便可以使用不同的目录调用函数并执行命令:

def task_speedtest(path):
    os.system("speedtest-cli >> " + path)

def task_ping():
    os.system("ping www.google.com -c5 >> " + path)

path = "/Desktop/logs"
task_speedtest(path)
task_ping(path)

Regarding your second question, yes, there is a better module to use than os.system . 关于第二个问题, 是的,有一个比os.system更好的模块可以使用

There exists an upgraded version of os.system which is Subprocess , according to the official Python documentation (Python 3.6) : 根据官方Python文档(Python 3.6) ,存在os.system的升级版本,即Subprocess

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. 子流程模块允许您生成新流程,连接到其输入/输出/错误管道,并获取其返回代码。 This module intends to replace several older modules and functions. 该模块旨在替换一些较旧的模块和功能。

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. 推荐的调用子流程的方法是将run()函数用于它可以处理的所有用例。

 subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None) 

Run the command described by args. 运行args描述的命令。 Wait for command to complete, then return a CompletedProcess instance 等待命令完成,然后返回CompletedProcess实例

There is even a section on how to replace os.system with the new subprocess here : 甚至还有关于如何更换部分os.system新子在这里

sts = os.system("mycmd" + " myarg")
# becomes
sts = call("mycmd" + " myarg", shell=True)

I suggest you read more about the new module in the official Python documentation of Subprocess here: https://docs.python.org/3.6/library/subprocess.html 我建议您在此处阅读Subprocess的官方Python文档中有关新模块的更多信息: https : //docs.python.org/3.6/library/subprocess.html

Use the speedtest-cli API as detailed in the wiki 使用wiki中详细介绍的speedtest-cli API

(following code from the wiki) (以下来自Wiki的代码)

import speedtest

servers = []
# If you want to test against a specific server
# servers = [1234]

s = speedtest.Speedtest()
s.get_servers(servers)
s.get_best_server()
s.download()
s.upload()
s.results.share()

results_dict = s.results.dict()

for pinging in python, see this question and many answers 在python中执行ping操作,请参阅此问题和许多答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改 speedtest.py 和 speedtest-cli 的 output 以将 IP 地址包含在 output.Z628Z19675FFE88FEF3F 文件中 - Changing output of speedtest.py and speedtest-cli to include IP address in output .csv file 不同界面上的 Speedtest-cli - Speedtest-cli on a different interface speedtest-cli 在控制台中工作,但不能作为脚本使用 - speedtest-cli works in console, but not as script 在 Python3 脚本中使用 speedtest-cli 来测量互联网速度 - 收到错误消息“No module named speedtest” - Using speedtest-cli within a Python3 script to measure internet speed - getting error message 'No module named speedtest' 我如何在python脚本中使用speedtest-cli或任何替代方法而不是命令行? - How do i use speedtest-cli or any alternative in python script instead of command line? 有没有办法在速度测试期间使用 Python 的 speedtest-cli 库实时绘制实时下载/上传速度? - Is there a way to graph the real-time download/upload speed real time during a speed test using the speedtest-cli library for Python? 在Python脚本中循环运行并记录“ ping”的输出 - Run and log the output of 'ping' in a loop in a Python script 将终端 output 记录到文本文件 python - Log terminal output to a text file python python程序在终端和文件中的输出结果 - output result of python program in both terminal and file 是否可以在python中的终端中创建一个日志文件? - Is it possible to create a log file in the terminal in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM