I have the curl command, but not sure about how to run that in python script.
curl -H "Content-Type: application/json" -u "username:password" -d '{ "name":"something" }' "https://xxxxxxxx"
I'm planning to use subprocess, but the api documents aren't very helpful.
Also does anyone know how to get the sectionId from testrail?
Bill from TestRail here. You can find a link to our Python bindings here:
http://docs.gurock.com/testrail-api2/bindings-python
Regarding getting the section ID, you can use the get_sections method for a project/suite to return all the section details including IDs. You can find more info on that here:
http://docs.gurock.com/testrail-api2/reference-sections#get_sections
If you're looking for the section ID for a specific test case, you can get that from the get_case method.
You probably want to use the requests package for this. The curl command translates to something like this:
import json
import requests
response = requests.post('https://xxxxxxxx',
data=json.dumps({'name': 'something'}),
headers={'Content-Type': 'application/json'},
auth=('username', 'password'))
response_data = response.json()
If you really want to use subprocess, you can do something like this:
import subprocess
curl_args = ['curl', '-H', 'Content-Type: application/json', '-u', 'username:password',
'-d', '{ "name":"something" }', 'https://xxxxxxxx']
curl_output = subprocess.check_output(curl_args)
I consider the latter approach less "Pythonic".
Related
I would like to send a complex curl command into python (3.9.7) script to download an output file .tif
I use a TimeNum variable because i want to download different files and clip the .tif in a personal area of intrest (I want to use a for loop).
The curl command is something like this:
curl --data '{"productType":"VMI","productDate":"1648710600000"}' -H "Content-Type: application/json" -X POST https://example.it/wide/product/downloadProduct --output hrd2.tif
I try different solutions:
1)
import shlex
import subprocess
TimeNum=1648710600000
cmd ='''curl --data \'{"productType":"VMI","productDate":"%s"}\' -H "Content-Type: application/json" -X POST https://example.it/wide/product/downloadProduct --output %s.tif''' % (TimeNum,TimeNum)
args = shlex.split(cmd)
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
The download work but the output takes up 1KB instead of about 11MB, and an error occur when i try to open it in Windows.
I write a txt file with a list of curl command line by line and then:
file = open('CURL_LIST.txt', 'r')
lines = file.readlines()
for line in enumerate(lines):
os.system(line.strip())
but don't work fine and i have the same output of case 1 above.
Now i try to use urllib.request but i'm not able to use it very well.
Someone have a suggestion?
Thanks in advance for your help
Important info
There is a self-signed certificate on that server so you get a warning (and this is also the reason why you get small files).
In the example below I disabled certificate checking but this is DANGEROUS and you should use it only if you understood the risks and it is ok for you anyway (e.g. you are the owner of example.it).
Given the nature of example.it I'd assume that you are just using it only for learning purpose but please be careful and read more about the risks of self-signed certificates anyway.
The proper solution from a risk / security standpoint for a similar problem is NOT connecting to such a server.
Once this is clear, just for the sake of testing / learning
I would suggest using Python's requests library (please note the verify=False to disable certificate check):
import requests
time_num = 1648710600000
headers = {
# Already added when you pass json=
# 'Content-Type': 'application/json',
}
json_data = {
'productType': 'VMI',
'productDate': time_num,
}
response = requests.post('https://example.it/wide/product/downloadProduct', headers=headers, json=json_data, verify=False)
with open(time_num + '.tif', 'wb') as f:
f.write(response.content)
If you prefer the approach you posted it is possible to disable cert check also in curl (-k option):
import shlex
import subprocess
TimeNum=1648710600000
cmd ='''curl -k --data \'{"productType":"VMI","productDate":"%s"}\' -H "Content-Type: application/json" -X POST https://example.it/wide/product/downloadProduct --output %s.tif''' % (TimeNum,TimeNum)
args = shlex.split(cmd)
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
I am writing a script in Python that detects the language of a provided text.
I found the following command line that works in a terminal, but I would like to use it in my script.
Command :
**curl -X POST "https://api.cognitive.microsofttranslator.com/detect?api-version=3.0" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json" -d "[{'Text':'What language is this text written in?'}]"**.
In the script, elements like the client-secret, the "text", and so on... should be in variables. And I would like to catch the result of the whole command line in a variable and then print it to the user.
How can I do this?
I found the command line here.
The command in Command Line is essentially sending http request. So you just need to use the python code I provide below, just for reference.
import requests
import json
url = 'https://api.cognitive.microsofttranslator.com//Detect?api-version=3.0'
body =[{"text": "你好"}]
headers = {'Content-Type': 'application/json',"Ocp-apim-subscription-key": "b12776c*****14f5","Ocp-apim-subscription-region": "koreacentral"}
r = requests.post(url, data=json.dumps(body), headers=headers)
result=json.loads(r.text)
a=result[0]["language"]
print(r.text)
print("Language = " + a)
I am trying to make a post request using curl in python but the below script throws error
import os
first_name1 = "raj"
last_name1 = "kiran"
full_name = "raj kiran"
headline = "astd"
location1 = "USA"
current_company1 = "ss"
curl_req = 'curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'.format(first_name1,last_name1,current_company1,headline,location1,full_name)
os.popen(curl_req)
Error:
SyntaxError: invalid syntax
How to make above program work?
The problem in your code is the quotes. Change it to:
curl_req = '''curl -X POST -H "Content-Type: application/json" -d '{"first_name":"{0}","last_name":"{1}","current_company":"{2}","title":"{3}","campus":"","location":"{4}","display_name":"{5}","find_personal_email":"true"}' http://localhost:8090'''.format(first_name1,last_name1,current_company1,headline,location1,full_name)
But, as mentioned in the comments, requests will always be a better choice.
Requests syntax:
import requests
post_data = {
# all the data you want to send
}
response = requests.post('http://localhost:8090', data=post_data)
print response.text
One good resource I've used for converting a cURL request to Python requests is curlconverter. You can enter your cURL request and it will format it for Python requests.
As a side note, it can also convert for PHP and Node.js.
Hope that helps!
I want to execute a curl command in Python.
Usually, I just need to enter the command in the terminal and press the return key. However, I don't know how it works in Python.
The command shows below:
curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
There is a request.json file to be sent to get a response.
I searched a lot and got confused. I tried to write a piece of code, although I could not fully understand it and it didn't work.
import pycurl
import StringIO
response = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere')
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.POSTFIELDS, '#request.json')
c.perform()
c.close()
print response.getvalue()
response.close()
The error message is Parse Error. How to get a response from the server correctly?
For the sake of simplicity, you should consider using the Requests library.
An example with JSON response content would be something like:
import requests
r = requests.get('https://github.com/timeline.json')
r.json()
If you look for further information, in the Quickstart section, they have lots of working examples.
For your specific curl translation:
import requests
url = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere'
payload = open("request.json")
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r = requests.post(url, data=payload, headers=headers)
Use curlconverter.com. It'll convert almost any curl command into Python, Node.js, PHP, R, Go and more.
Example:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/asdfasdfasdf
becomes this in Python
import requests
json_data = {
'text': 'Hello, World!',
}
response = requests.post('https://hooks.slack.com/services/asdfasdfasdf', json=json_data)
curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
its Python implementation looks like this:
import requests
headers = {
'Content-Type': 'application/json',
}
params = {
'key': 'mykeyhere',
}
with open('request.json') as f:
data = f.read().replace('\n', '')
response = requests.post('https://www.googleapis.com/qpxExpress/v1/trips/search', params=params, headers=headers, data=data)
Check this link, it will help convert cURL commands to Python, PHP and Node.js
import requests
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere"
data = requests.get(url).json
maybe?
if you are trying to send a file
files = {'request_file': open('request.json', 'rb')}
r = requests.post(url, files=files)
print r.text, print r.json
ahh thanks #LukasGraf now i better understand what his original code is doing
import requests,json
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere"
my_json_data = json.load(open("request.json"))
req = requests.post(url,data=my_json_data)
print req.text
print
print req.json # maybe?
My answer is WRT python 2.6.2.
import commands
status, output = commands.getstatusoutput("curl -H \"Content-Type:application/json\" -k -u (few other parameters required) -X GET https://example.org -s")
print output
I apologize for not providing the required parameters 'coz it's confidential.
I had this exact question because I had to do something to retrieve content, but all I had available was an old version of Python with inadequate SSL support. If you're on an older MacBook, you know what I'm talking about. In any case, curl runs fine from a shell (I suspect it has modern SSL support linked in) so sometimes you want to do this without using requests or urllib.request.
You can use the subprocess module to execute curl and get at the retrieved content:
import subprocess
# 'response' contains a []byte with the retrieved content.
# use '-s' to keep curl quiet while it does its job, but
# it's useful to omit that while you're still writing code
# so you know if curl is working
response = subprocess.check_output(['curl', '-s', baseURL % page_num])
Python 3's subprocess module also contains .run() with a number of useful options.
I use os library.
import os
os.system("sh script.sh")
script.sh literally only contains the curl.
PYTHON 3
Only works within UNIX (Linux / Mac) (!)
Executing a cURL with Python 3 and parsing its JSON data.
import shlex
import json
import subprocess
# Make sure that cURL has Silent mode (--silent) activated
# otherwise we receive progress data inside err message later
cURL = r"""curl -X --silent POST http://www.test.testtestest/ -d 'username=test'"""
lCmd = shlex.split(cURL) # Splits cURL into an array
p = subprocess.Popen(lCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate() # Get the output and the err message
json_data = json.loads(out.decode("utf-8"))
print(json_data) # Display now the data
Sometimes you also need to install these dependencies on UNIX if you experience strange errors:
# Dependencies
sudo apt install libcurl4-openssl-dev libssl-dev
sudo apt install curl
use requests lib.. this code is :
curl -LH "Accept: text/x-bibliography; style=apa" https://doi.org/10.5438/0000-0C2G
equal to this:
import requests
headers = {
'Accept': 'text/x-bibliography; style=apa',
}
r = requests.get('https://doi.org/10.5438/0000-0C2G', headers=headers)
print(r.text)
if you os supporting curl you can do something like this:
import os
os.system("curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere")
I'm using this way... And I think you can use this too!
by the way.. the module "os" is auto-installing when you install python.
soo, you don't need to install packages ;)
This is one approach:
Import os
import requests
Data = os.execute(curl URL)
R= Data.json()
I am using Requests to communicate with a simple api.
The code looks like this:
payload = {'text': reply, 'name': varname, 'avatar': varavatar}
r = requests.get('http://example.de/create.json', params=payload)
However, the result looks like this:
{"result": "error", "error": "error_no_direct_connection"}
This might be a problem with the api but I'm not sure.
So I printed out the URL that Requests created (print r.url).
It looks like this: http://url.com/create.json?text=Test&name=myname&avatar=http%3A%2F%2Fa0.url.com%2Fnormal.png
To make sure it's not a problem oft the api I tried it with curl. The command works!
curl --data-urlencode "text=Test" -d "name=myname" -d "avatar=http://url.com/normal.png" http://url.com/create.json
The curl option --data causes curl to submit a POST request, not a GET request. Try request.post(..., data=...).