简体   繁体   English

适用于https调用curl命令的等效python代码

[英]Equivalent python code for curl command for https call

I have a curl command that works and gives me back the JSON. 我有一个curl命令可以正常工作,并且还给我JSON。 Curl command: 卷曲命令:

    curl -sS -k -L -H "Authorization: bearer <token>" -X GET https://IP:PORT/api/v1/namespaces

I tried with requests and pycurl modules which I found in the other posts but no luck. 我尝试了在其他帖子中找到的requestspycurl模块,但是没有运气。

Can anyone help me with finding the equivalent in python??? 任何人都可以帮助我在python中找到等效项吗???

We can do this with requests like this: 我们可以这样处理:

import requests
header = {'Authorization': 'bearer <token>'}
resp = requests.get("https://IP:PORT/api/v1/namespaces", 
    headers = header, 
    verify=False)
print(resp.status_code)
print(resp.text)
  • The -H switch behaviour is replicated by sending a header 通过发送标头来复制-H开关行为
  • The -L switch behaviour is replicated by specifying verify=False 通过指定verify=False复制-L开关行为
  • The -sS and -k are about the behaviour of curl and not relevant here -sS-k与卷曲行为有关,此处不相关
  • The -X GET is replicated by using requests.get() 通过使用requests.get()复制-X GET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM