Search code examples
python-3.xcurlsubprocesspopen

download a file with curl command using Popen subprocess


I am using curl command to download a file and popen subprocess to run that command . i want to control the limit rate of downloading to 50mps.

when i run below commad in terminal, it works fine.

curl -o down.tar.gz --limit-rate 50M http://localhost:5000/download

but when i run Popen to run that command in pycharm like below,

Popen(['curl', '-o', 'down.tar.gz','--limit-rate 50M', 'http://localhost:5000/download'])

then it gives me error as shown below,

curl: option --limit-rate 50M: is unknown
curl: try 'curl --help' or 'curl --manual' for more information

How i can use curl with limit rate in Popen ??


Solution

  • When Popen is called with a list, the arguments are not split further. So your code is passing the whole --limit-rate 50M as a single argument to curl.

    You need to split the option & option-value in the command token list like so:

    Popen(['curl', '-o', 'down.tar.gz','--limit-rate', '50M', 'http://localhost:5000/download'])