-1

I have a problem, I need the output of the command in python and exactly get the download speed from the wget command. My code is :

#!/usr/bin/python3


import os
import re
import subprocess

command = "wget ftp://ftp:password@172.17.1.129:1111/test.bin -p -nv "
process = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
ftp_result = re.findall(('\d+.\d+'),process)
        
print (ftp_result)

TypeError: cannot use a string pattern on a bytes-like object

What I'm doing wrong?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
homcioq
  • 15
  • 4

1 Answers1

0

On Python 3, subprocess returns bytes unless you explicitly request decoding.

Also, you generally want to avoid shell=True if you are not actually using the shell for anything.

#!/usr/bin/python3

# import os  # not used
import re
import subprocess

process = subprocess.check_output(
    ["wget", "ftp://ftp:password@172.17.1.129:1111/test.bin", "-p", "-nv"],
    text=True)   # this is the beef
ftp_result = re.findall(('\d+.\d+'),process)
        
print (ftp_result)

Notice also how check_output already defaults to capturing the output, so you don't need to explicitly direct stdout to a subprocess.PIPE, either.

Perhaps a more robust solution would be to switch to curl, which allows you to separately retrieve just the download speed using a format specifier.

tripleee
  • 175,061
  • 34
  • 275
  • 318