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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值