简体   繁体   中英

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; eg: speed test and 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 . 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?

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 .

There exists an upgraded version of os.system which is Subprocess , according to the official Python documentation (Python 3.6) :

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.

 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. Wait for command to complete, then return a CompletedProcess instance

There is even a section on how to replace os.system with the new subprocess here :

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

Use the speedtest-cli API as detailed in the wiki

(following code from the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM