Curl命令到python-requests方法的转换

rur96b6h  于 2022-11-13  发布在  Python
关注(0)|答案(1)|浏览(362)

我正在编写一个python脚本来转换一些Curl命令,以便可以使用python的requests模块中的方法来调用它们,但是我遇到了一些错误。
下面是curl命令块:

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

下面是我编写的名为“change_slice_allocation.py”的python脚本:

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)

我收到以下错误:
`

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

`
我是curl命令的新手。有人能帮我做正确的转换吗?谢谢。

disbfnqx

disbfnqx1#

我是curl命令的新手。有人能帮我做正确的转换吗?
下面是您发布的两个curl命令的转换。
我使用了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)

相关问题