3

I'm running the following curl command in CGI script in python using os.system call as below:

os.system('curl -u username:password -X PUT example.com/data/data1.txt -T /diskless/desktop/file.txt\r\n')

Getting the below error when I run the CGI script:

'!rl: Can't open '/diskless/desktop/file.txt') curl: try 'curl --help' or 'curl --manual' for more information.

Any suggestions please?

4
  • do you specifically require curl or can you not use other python libraries to handle http requests for you (like python requests for example) Commented May 15, 2017 at 9:15
  • I want to upload a file from my local directory to the http path that I specify. This will be executed in python CGI. Can you give me a sample code Commented May 15, 2017 at 9:22
  • look at the error - Can't open '/diskless/desktop/file.txt'. do you have permissions on the file you are trying to upload? Commented May 15, 2017 at 9:25
  • Yes, I have. It's the problem with '\r\n' at the end of the command. This '\r\n' is required if you are executing it in a CGI script; otherwise you would get a malformed header error. Commented May 15, 2017 at 9:36

1 Answer 1

10

you can use subprocess

import subprocess
command = 'curl -u username:password -X PUT example.com/data/data1.txt -T /diskless/desktop/file.txt\r\n'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
Sign up to request clarification or add additional context in comments.

8 Comments

@lliyan Markov- Any idea on why os.systme() is getting failed if I use '\r\n' at the end of the command. Also, your subprocess.POPEN is getting the following error : p = subprocess.POPEN(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) AttributeError: 'module' object has no attribute 'POPEN'
Sorry, lliyan. There's no error now. But, I don;t see file getting uploaded.
do you actually need to have \r\n at the end of that command?
If we are executing curl command in CGI script using os.system, it's required, otherwise you would get a malformed header error.
try that: command = curl -X POST -d @/diskless/desktop/file.txt http://username:[email protected]/uploadlink/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.