Home > database >  How to run Curl commands from python script using subprocess module
How to run Curl commands from python script using subprocess module

Time:04-13

I want to run curl function using subprocess module in python. But it is not working. Here's the code for it.

import subprocess

CURL_CMD = 'C:/Users/eureka/curl/bin/curl.exe'

curl  = [CURL_CMD]   ['curl', '-F', '"userfile=@/C:/Users/eureka/Desktop/Test/hello.txt"', '"localhost:5000/fileserver"']
cmd = (' '.join(curl))
print(cmd)

process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(process.stdout)
print(process.stderr)

But it gives me a bunch of error stating.

FileNotFoundError: [WinError 2] The system cannot find the file specified

So Here I created a local file upload server using flask. When I run the below command in the git bash it runs completely fine with status code 200.

curl -F "userfile=@/C:/Users/eureka/Desktop/Test/hello.txt" "localhost:5000/fileserver"

But when I run the the same command in cmd it shows me error.

curl: (26) Failed to open/read local data from file/application

And I tried with both forward and backward slashes thinking if that's the problem. I also tried running from the same directory where hello.txt file is there, but the same error.

FOR PYTHON SCRIPT:

I also tried replacing the curl assignment line in the python script with this.

curl  = ['curl', '-F', '"userfile=@/C:/Users/eureka/Desktop/Test/hello.txt"', '"localhost:5000/fileserver"']

But it also shows the same error.

b'curl: (26) Failed to open/read local data from file/application\r\n'

So as it was working with git bash externally I thought of providing the git address instead of curl address in the python script like this. But the scripts runs infinitely and does nothing.

GIT_BASH = 'C:/Users/eureka/AppData/Local/Programs/Git/bin/git.exe'
curl  = [GIT_BASH]   ['curl', '-F', '"userfile=@/C:/Users/eureka/Desktop/Test/hello.txt"', '"localhost:5000/fileserver"']

So the thing is it is neither running from the python script nor from cmd, but working fine in git. The main thing for me is that the python script should work and I want it to work using the suprocess module. I'm not able to find any solution on this so asking for help.

CodePudding user response:

The curl command needs to be corrected here to be able to run in cmd and python script.

curl -F userfile=@"C:/Users/eureka/Desktop/Test/hello.txt" "localhost:5000/fileserver"

This above command will work in the cmd. Only the file path needs to be enclosed in double quotes in this case the value of userfile.

To run in the python script using the subprocess module. Just write the following code.

import subprocess

curl  = ['curl', '-F', r'userfile=@"C:\Users\eureka\Desktop\Test\hello.txt"', '"localhost:5000/fileserver"']
cmd = ' '.join(curl)
print(cmd)

process = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

Just remember to include r before the string i.e. r'userfile=@"C:\Users\eureka\Desktop\Test\hello.txt"' to make it a raw string.

Raw string in python : If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote.

  • Related