python用request实现curl -u 基于httpBasicuthentication认证

需求

通过python实现请求请求携带用户名和密码来登录 clouderaManager
curl -u <username>:<password> 'http://10.11.11.11:7180/api/v17/clusters/cluster/services/impala/impalaQueries?filter=(queryState=EXCEPTION)'
用python request实现

解决方案

代码如下

import requests

s = requests.session()

s.auth = ('用户名', '密码')

res = s.get("URL")
#获取请求的json数据
data = res.json()

还有更简单的

from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))

相关资料

  • https://2.python-requests.org/en/latest/api/#requests.auth.HTTPBasicAuth
  • https://www.oschina.net/question/1162040_239895
  • https://docs.python-requests.org/zh_CN/latest/user/authentication.html#id2
posted @ 2022-11-10 19:26  彬在俊  阅读(291)  评论(0)    收藏  举报