Reputation: 341
I'm trying to make a request, in python, from a VM running Ryu controller to a VM running a openvswitch. I've tested said request, and it works when I execute it in a terminal (a string is supposed to be returned):
curl -X POST -d '{"priority": 500, "match": {"in_port": 3}}' http://localhost:8080/stats/flow/8796748823560
This was my first try in python:
import subprocess
proc = subprocess.run(['curl', '-X', 'POST', '-d', '\'{"priority": 500, "match": { "in_port": 3} }\'','http://localhost:8080/stats/flow/8796748823560'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
response = proc.stdout.decode('utf-8')
A simple POST, as you can see. However, the response is always " ", showing the error:
curl: (3) URL using bad/illegal format or missing URL
Then, I decided to use the requests python library and wrote the POST the following way:
import requests
data = '{ "priority": 500, "match": {"in_port": 3}}'
response = requests.post('http://localhost:8080/stats/flow/234', data=data)
However, I don't know where to put the option -X. In the library documentation, I cannot find the right place to put it, if there is any.
I need help to understand where I should place that -X option in the code and, if it is not possible, how could I execute that curl on python (I was trying to avoid the shell=True
flag on subprocess, as I don't think it is safe).
Upvotes: 0
Views: 644
Reputation: 133849
The -X
/--request
in curl is an option that is followed by the HTTP verb to be used in the request. Since this is followed by POST
it means that a POST
request should be used. In fact -X POST
is not needed since the mere presence of -d
should cause curl
to make a HTTP POST request.
Thus, use of request.post
with data
containing the body should be sufficient.
Upvotes: 1