How to run delete webrequest using Python? - python

Currently, I use Powershell to clear cache using the following command:
Invoke-WebRequest https://url -method DELETE
How can I make my Python script do to the same type of delete web request?

This seemed to work:
import requests
url = "https://myurl"
response = requests.delete(url)

Related

How to run cURL request using python

I would like to know how to run the following cURL request using python (I'm working in Jupyter notebook):
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"
I've seen some similar questions and answers suggesting using "requests.get", but I am a complete python newbie and am not sure how to structure the syntax for whole request including the id, secret and token elements. Any help would be really appreciated.
Thanks!
no need to use curl. use the below
import requests
graph_api_version = 'a'
app_id = 'b'
app_secret = 'c'
your_access_token = 'd'
url = f"https://graph.facebook.com/{graph_api_version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={your_access_token}"
r = requests.get(url)
Convert your Curl request to Python instantly here :
https://curl.trillworks.com/
you can use some lib that run commands in python like subprocess. for example: subprocess.run(["curl", "google.com"])

Call API in python to run Jenkins Job - problem with authentication

I have got a jenkins job which i can run with making a post request:
curl -u albert405:{mytoken} http://172.31.32.33:8080/job/URL_Job_Trigger/build?token=ozSVoEQfLg
Could you tell me how to place this authentication (albert405:{mytoken}) into my python script:
import requests
url = 'http://172.31.32.33:8080/job/URL_Job_Trigger/build?token=ozSVoEQfLg'
x = requests.post(url)
print(x.text)
I have managed to solve it by this :
http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN#YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build
import requests
build = requests.post("http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN#YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build?token=TokenName")
Where is your authentication code? I see none of it.
Jenkins uses Basic Auth, which is indicated here https://wiki.jenkins.io/display/JENKINS/Remote+access+API.
In order to send auth parameters with requests it's a simple:
res =requests.post(url, auth=("albert405", "password"))
Which is the first documentation you get when googling basic auth requests: https://2.python-requests.org/en/master/user/authentication/

Python : Passing username password to fetch a url response in json using method GET

Scenario:
I have a python code which gives me exact results in windows 10 OS but when I run the same in REDHAT 6.4 it throws an error.
I am trying to open a API URL and fetch the json content and further manipulate it.
Here is the code:
import json
import urllib
url = 'https://#username:password#api.locu.com/v1_0/venue/search'
data = json.load(urllib.urlopen(url)
print data
When I run this code in windows10 platform with python version 2.7.13 it gives me exact output, but the same when run in REDHAT 6.4 platform gives an error related to authentication inturn it returns me with no json data.
Can't I use the format below or I need to pass the authentication as headers?
https://#username:password#apiurl
If I need to pass the authentication as headers how can I do that?
*note: I am using the Redhat OS in a VM.
Not sure if this is proxy issue.

Run a curl tlsv1.2 http get request in python?

I have the following command that I run using curl in linux.
curl --tlsv1.2 --cert ~/aws-iot/certs/certificate.pem.crt --key ~/aws-iot/certs/private.pem.key --cacert ~/aws-iot/certs/root-CA.crt -X GET https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow
This command returns JSON text that I want. However I want to be able to run the above command in Python3. I do not know what library to use in order to get the same JSON response.
P.S. I replace "data" with my account number in AWS to get JSON
After playing around with it on my own I was able to successfully do it in python using the requests library.
import requests
s = requests.Session()
r = s.get('https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow',
cert=('/home/pi/aws-iot/certs/certificate.pem.crt', '/home/pi/aws-iot/certs/private.pem.key', '/home/pi/aws-iot/certs/root-CA.crt'))
print(r.text)

Python HTTPS client with basic authentication via proxy

From Python, I would like to retrieve content from a web site via HTTPS with basic authentication. I need the content on disk. I am on an intranet, trusting the HTTPS server. Platform is Python 2.6.2 on Windows.
I have been playing around with urllib2, however did not succeed so far.
I have a solution running, calling wget via os.system():
wget_cmd = r'\path\to\wget.exe -q -e "https_proxy = http://fqdn.to.proxy:port" --no-check-certificate --http-user="username" --http-password="password" -O path\to\output https://fqdn.to.site/content'
I would like to get rid of the os.system(). Is that possible in Python?
Proxy and https wasn't working for a long time with urllib2. It will be fixed in the next released version of python 2.6 (v2.6.3).
In the meantime you can reimplement the correct support, that's what we did for mercurial: http://hg.intevation.org/mercurial/crew/rev/59acb9c7d90f
Try this (notice that you'll have to fill in the realm of your server also):
import urllib2
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password(realm='Fill In Realm Here',
uri='https://fqdn.to.site/content',
user='username',
passwd='password')
proxy_support = urllib2.ProxyHandler({"https" : "http://fqdn.to.proxy:port"})
opener = urllib2.build_opener(proxy_support, authinfo)
fp = opener.open("https://fqdn.to.site/content")
open(r"path\to\output", "wb").write(fp.read())
You could try this too:
http://code.google.com/p/python-httpclient/
(It also supports the verification of the server certificate.)

Categories