How to run Curl commands from python script using subprocess module
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.
Comments
Post a Comment