What is the equivalent of this CURL command in python? - python

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'})

Related

Attempts to use curl in conjunction with api give 404 error

I am attempting to query Confluence knowledge base pages using the API and failing.
I have researched the api and have the following:
Browsing Content
curl -u admin:admin http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool
Read Content and Expand the Body:
curl -u admin:admin http://localhost:8080/confluence/rest/api/content/3965072?expand=body.storage | python -mjson.tool
What I actually want to do is dump out the contents of a page / pages in a "Space" as Confluence calls it to a file, or on screen.
This is an actual page:
"https://bob.atlassian.net/wiki/spaces/BKB/pages/1234567/BOB+KNOWLEDGE+BANK"
And this is an example I found of using the REST API to do what I want:
curl -u admin:admin -G "http://myinstance.address:8090/rest/api/content/search" --data-urlencode "cql=(type=page and space=low and title ~ "LOWERTOWN")" \ | python -m json.tool
How I am translating what I have researched:
curl -u "bob#bob.com:12345678" -G "https://bob.atlassian.net/rest/api/content/search" --data-urlencode "cql=(type=page and space=BKB and title ~ "BOB+KNOWLEDGE+BANK")" \ | python -m json.tool
Which results in this error:
curl: (3) URL using bad/illegal format or missing URL
No JSON object could be decoded
I have grabbed my logic here from this site:
https://community.atlassian.com/t5/Confluence-questions/How-to-get-Conflunece-knowledge-base-data-through-API/qaq-p/1030159
And I am assuming I have misunderstood this:
/rest/api/content/search
And where it belongs in my curl statement and linking it to the knowledge base. I am also unsure if applying the -mjson.tool is applicable in my case or if I actually have it installed / need to verify that.
Can you help me interpret this correctly?
You are almost there! You just need to pass cql as query parameter to the service as mentioned here in Atlassian documentation:
curl --request GET
--url 'https://your-domain.atlassian.net/wiki/rest/api/search?cql={cql}'
--header 'Authorization: Bearer <access_token>'
--header 'Accept: application/json'

Send curl POST Request in Python without installing a lib

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)

Execute curl command and fetch response

I have a curl command. I want to fetch the response after executing it in python 3.
curl https://api.abc.com/targets/targetID\
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: JWT PROBELY_AUTH_TOKEN"
How can I execute the script in Python and get the response ?
try this:
import requests
url = 'https://api.abc.com/targets/AxthamE0v3E-/scans/S6dOMPj8SsoH/'
r = requests.get(url,headers={"Content-Type": "application/json","Authorization": "JWT PROBELY_AUTH_TOKEN"})
print(r.text)
If you need to perform a request, you can use the library requests.
You can easily install it with pip install requests
https://www.w3schools.com/python/module_requests.asp
You can use this link to see how to work with this library

How to run Curl Command in Python

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/

how to use CURL commands in PYTHON

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.

Categories

Resources