Search code examples
pythoncurlcgi

Running Curl command in python


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?


Solution

  • 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()