I want to send this command in a python program. How can I do this? I do not need to print any response.
curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'
Using os:
from os import system
system("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'")
Using subprocess:
subprocess.Popen("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'", shell=True)
You can use the shell module to run such commands neatly:
>>> from shell import shell
>>> curl = shell("curl -k -X PUT 'http://10.210.12.158:10065/iot/put_bulb/true?id=4'")
>>> curl.output()
Alternatively, I'd suggest using the requests module for making such http requests from Python.
Related
I wanna send a curl post request in python. But I can't install any lib like 'request'. I could send POST request like following example :
curl -H "Content-Type: application/json" -X POST -d {\"username\":\"myusername\",\"password\":\"mypassword\"} https://example.com/login
I need equal code as the above in python2. Then, i must read what it returns. I'm working on Windows 10.
Write a curl command in a way that it can work in the shell and then execute the command in the shell.
This way you don't need the requests package.
import os
command = "curl -u {username}:{password} [URL]"
os.system(command)
I want to run Following Curl Command in Python and I was trying the requests command of python but I am not getting the proper results.
curl -v ftp://ftp.abc.com/File.txt --socks5-hostname socks.abc.com:PORT --user username:password -O
You can use subprocess.
import subprocess
result = subprocess.run(["curl", "-v", "ftp://ftp.abc.com/File.txt --socks5-hostname socks.abc.com:PORT --user username:password -O"], capture_output=True)
print(result.stdout)
I'm not into web development but this looks like something the requests module should be able to handle.
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
https://requests.readthedocs.io/en/master/
If I use the following command in the Git Bash, it works fine. The Output from the curl are write into the file output.txt
curl -k --silent "https://gitlab.myurl.com/api/v4/groups?page=1&per_page=1&simple=yes&private_token=mytoken&all?page=1&per_page=1" > output.txt
Python Code:
import subprocess, shlex
command = shlex.split("curl -k --silent https://gitlab.myurl.com/api/v4/groups?page=1&per_page=1&simple=yes&private_token=mytoken&all?page=1&per_page=1 > output.txt")
subprocess.Popen(command)
The Python code write nothing in my file "output.txt".
How can I write in the output.txt or get the Output direct in Python?
You cannot use redirection directly with subprocess, because it is a shell feature. Use check_output:
import subprocess
command = ["curl", "-k", "--silent", "https://gitlab.myurl.com/api/v4/groups?page=1&per_page=1&simple=yes&private_token=mytoken&all?page=1&per_page=1"]
output = subprocess.check_output(command)
You could use the command in another way in order to write to output.txt:
curl -k --silent "https://gitlab.myurl.com/api/v4/groups?page=1&per_page=1&simple=yes&private_token=mytoken&all?page=1&per_page=1" --output output.txt
Also you should consider that output.txt might not be saved in the directory you expect so I would also advice you to name output.txt in another way, a unique one, then update the locate linux command database (see updatedb command) and then search the file with locate.
PS: all this make sense when you need the writing to output.txt (your question accepts this situation too, so I hope it helps)
I am trying to connect to an API with kerberos authentification.
With CURL its just simple:
curl -k --negotiate -u : "https://MY-API-SERVER" -i
With Python I just can't find how to do the samething.
What I tried so far is:
from requests_kerberos import HTTPKerberosAuth, REQUIRED
kerberos_auth = HTTPKerberosAuth(mutual_authentication=REQUIRED,force_preemptive=True)
r = requests.get("https://MY-API-SERVER", auth=kerberos_auth,verify=False, headers={'username':'xxxxx', 'password':'yyyyy'})
This is the command I am running in bash:
curl -d "username=xxxxxx" -d "password=xxxxx" <<address>> --insecure --silent
How can I run this using Python?
Try Subprocess:
import subprocess
# ...
subprocess.call(["curl -d \"username=xxxxxx\" -d \"password=xxxxx\" https://xxxxx.com/logreg/login/ --insecure --silent" , shell=True)
I did not try what I wrote, but the essential is here.
Check this page for more info : Subprocess management
I hope you don't want to explicitly run curl, but you just want to get the same result.
>>> import requests
>>> r = requests.get('https://example.com', auth=('user', 'pass'), verify=False)
>>> print(r.status_code)
200