我需要从经过LDAP认证的服务器获取数据。如果每个请求都需要LDAP身份验证,则非常耗时。 如何为所有请求进行一次身份验证? 我尝试在Python的请求库中使用会话,但是它不起作用。
import requests
from requests.auth import HTTPBasicAuth
class HttpRequests:
def __init__(self, collector):
self._session = requests.Session()
self._collector = collector
print("Init session to {}".format(collector._server))
self._session.post(collector._server, auth=(self._collector._user, self._collector._passwd), verify=(not self._collector._insecure))
def getHttpResponse(self, my_url):
# make a GET request
# response = self._session.get(my_url, auth=(self._collector._user, self._collector._passwd), verify=(not self._collector._insecure)) \
# if self._collector._user and self._collector._passwd else self._session.get(my_url, verify=(not self._collector._insecure))
response = self._session.get(my_url, verify=(not self._collector._insecure))
#Connection fail
if response.status_code != 200:
print('******************************************************************************************')
print(' Connection fail: ')
print(' - Status code: {}'.format(response.status_code))
print(' - Url: {}'.format(my_url))
print(' - Username: {}'.format(self._collector._user))
print(' - Password: {}'.format(self._collector._passwd))
print('******************************************************************************************')
return response