Search code examples
pythoncmd

Open a CMD shell and execute a command via Python


I'm having some problems with the following command:

> curl -d "{\"id\": 20, \"status\": 0}" -H "Content-Type: application/json" -X POST http://192.168.5.204:8080/Relay"

I'm trying to connect to a raspberry.

If I type this command directly on a CMD that I open personally, the command works fine:

works ok

When, instead, via Python I send this command

os.system('cmd /k "curl -d "{\"id\": 20, \"status\": 0}" -H "Content-Type: application/json" -X POST http://192.168.5.204:8080/Relay"')

seems that the command is not performed. I've added the /k in order to see what is going on on the shell and this is the error I have:

command sent via python

I'm not an expert but to me seems that in somehow it is not "building" the address in the correct way, or is something else?


Solution

  • I've found the solution to my problem by using the next code in Python.

    url="192.168.5.204:8080/Relay"
    payload = {'id': 20,'status': 1} 
    headers = {'Content-Type':'application/json'}
    requests.post(url, json=payload, headers=headers, allow_redirects=True)