Issue
I am writing a python script to convert some Curl commands so that they can be called using methods from requests module of python, but I got some errors.
Here is the curl commands block:
export NEXRAN_XAPP=`sudo kubectl get svc -n ricxapp --field-selector metadata.name=service-ricxapp-nexran-rmr -o jsonpath='{.items[0].spec.clusterIP}'`
curl -i -X PUT -H "Content-type: application/json" -d '{"allocation_policy":{"type":"proportional","share":1024}}' http://${NEXRAN_XAPP}:8000/v1/slices/slow
curl -i -X PUT -H "Content-type: application/json" -d '{"allocation_policy":{"type":"proportional","share":256}}' http://${NEXRAN_XAPP}:8000/v1/slices/fast
Here is the python script titled "change_slice_allocation.py" I have written:
import requests
import os
os.system ("export NEXRAN_XAPP=`kubectl get svc -n ricxapp --field-selector metadata.name=service-ricxapp-nexran-rmr -o jsonpath='{.items[0].spec.clusterIP}'`")
headers = {
'Content-type': 'application/json',
}
data1 = '{"allocation_policy":{"type":"proportional","share":1024}}'
data2 = '{"allocation_policy":{"type":"proportional","share":256}}'
response1 = requests.put('http://${NEXRAN_XAPP}:8000/v1/slices/fast', headers=headers, data=data1)
response2 = requests.put('http://${NEXRAN_XAPP}:8000/v1/slices/slow', headers=headers, data=data2)
I am getting following error:
`
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/usr/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 187, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f2a6b15ec40>: Failed to establish a new connection: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='$%7bnexran_xapp%7d', port=8000): Max retries exceeded with url: /v1/slices/fast (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f2a6b15ec40>: Failed to establish a new connection: [Errno -2] Name or service not known'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "change_slice_allocation.py", line 14, in <module>
response1 = requests.put('http://${NEXRAN_XAPP}:8000/v1/slices/fast', headers=headers, data=data1)
File "/usr/lib/python3/dist-packages/requests/api.py", line 131, in put
return request('put', url, data=data, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='$%7bnexran_xapp%7d', port=8000): Max retries exceeded with url: /v1/slices/fast (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f2a6b15ec40>: Failed to establish a new connection: [Errno -2] Name or service not known'))
`
I am new to curl commands. Can anybody help me to do the correct conversion? Thanks.
Solution
I am new to curl commands. Can anybody help me to do the correct conversion?
Here is a conversion of the two curl commands you posted.
I used Convert curl commands to Python
import requests
headers = {
# Already added when you pass json=
# 'Content-type': 'application/json',
}
json_data = {
'allocation_policy': {
'type': 'proportional',
'share': 1024,
},
}
response = requests.put('http://NEXRAN_XAPP:8000/v1/slices/slow', headers=headers, json=json_data)
import requests
headers = {
# Already added when you pass json=
# 'Content-type': 'application/json',
}
json_data = {
'allocation_policy': {
'type': 'proportional',
'share': 256,
},
}
response = requests.put('http://NEXRAN_XAPP:8000/v1/slices/fast', headers=headers, json=json_data)
Answered By - Misunderstood Answer Checked By - Cary Denson (WPSolving Admin)