-1
import os
os.system("wget -O /dev/null http://65.0.164.107/5MB.zip 2>&1 |grep -o '[0-9.]\+ [KM]*B/s'")

Here the above sample shows the download speed of the internet and I have to assign the result of the above to a variable and perform further operations. How can I save the above os.system to a variable?

for example, The above code displays the output as

5 mb/s

I wanna assign it to a variable

import os
a = os.system("wget -O /dev/null http://65.0.164.107/5MB.zip 2>&1 |grep -o '[0-9.]\+ [KM]*B/s'")
print(a)

It should output like

5 Mb/s or 5

I need solution to assign the output of that command to some variable and use it for further operations with it. I have tried many solutions but that's not working.

I just need to assign the command value to a variabl, any method in python will be fine.

Arunprasath
  • 57
  • 1
  • 7
  • Have you already tried to check this [answer](https://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on)? This question could be a duplicate – Elidor00 Jan 10 '22 at 15:14
  • It would probably be better to perform a speed test using native Python (e.g. urllib or requests) rather than run external executables such as wget. – jarmod Jan 10 '22 at 15:15
  • yes I have tried them, but they are resulting in error @Elidor00 – Arunprasath Jan 10 '22 at 15:16
  • @jarmod I need to try with wget not native python – Arunprasath Jan 10 '22 at 15:17

1 Answers1

0

You could take the output to a file and then access the file from your python script:

import os

a = os.system("wget -O /dev/null http://65.0.164.107/5MB.zip 2>&1 |grep -o '[0-9.]\+ [KM]*B/s' >> /path/to/file.txt")
speed = readFile("/path/to/file.txt")
Elidor00
  • 1,271
  • 13
  • 27