def http_get(self,ip,port,user,password,path,payload,auth_mode=0):
url = "http://{}:{}/{}".format(ip,port,path)
self.logger.debug("http_get: Sending: %s %s auth_mode=%d" % (url, payload, auth_mode) )
if auth_mode == 0:
auth = HTTPBasicAuth(user,password)
elif auth_mode == 1:
auth = HTTPDigestAuth(user,password)
else:
self.send_error("Unknown auth_mode '%s' for request '%s'. Must be 0 for 'digest' or 1 for 'basic'." % (auth_mode, url) )
return False
try:
response = requests.get(
url,
auth=auth,
params=payload,
timeout=5
)
# This is supposed to catch all request excpetions.
except requests.exceptions.RequestException as e:
self.send_error("Connection error for %s: %s" % (url, e))
return False
self.logger.debug("http_get: Got: code=%s", response.status_code)
if response.status_code == 200:
#self.logger.debug("http_get: Got: text=%s", response.text)
return response.text
elif response.status_code == 400:
self.send_error("Bad request: %s" % (url) )
elif response.status_code == 404:
self.send_error("Not Found: %s" % (url) )
elif response.status_code == 401:
# Authentication error
self.send_error(
"Failed to authenticate, please check your username and password")
else:
self.send_error("Unknown response %s: %s" % (response.status_code, url) )
return False
评论列表
文章目录