python请求nginx basic认证的页面
问题:
python在请求过程中会遇到nginx反向代理并通过basic设置了用户名密码校验的页面或者接口,此时直接requests请求回返回401,那么下面就用特别简单的办法来解决这个问题。
办法一:
@classmethod
def get_pod_info(cls):
req_url = cls.prometheus_url + '/api/v1/query?query=kube_pod_info'
prometheus_res = requests.get(url=req_url, auth=('prometheus', 'prometheus@MLI'))
print(prometheus_res.status_code)
return True
办法二:
import urllib2 theurl = 'http://127.0.0.1' username = 'admin' password = 'admin' passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, theurl, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) pagehandle = urllib2.urlopen(f'{theurl}/api/v1/query?query=kube_pod_info')
办法三:
适合服务器上使用(bash)
curl -u admin:admin http://127.0.0.1/api/v1/query?query=kube_pod_info -v
标签:python,urllib2,nginx,prometheus,opener,basic,query,pod From: https://www.cnblogs.com/LoveMoney-MrLi/p/16936219.html