2023-07-25 15:21:38 +02:00
|
|
|
from requests_ntlm import HttpNtlmAuth
|
|
|
|
from requests.auth import HTTPBasicAuth
|
|
|
|
from requests.auth import HTTPDigestAuth
|
|
|
|
from requests.sessions import Session
|
2023-08-02 13:13:43 +02:00
|
|
|
from .general import debug_dict
|
2023-07-25 15:21:38 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Auth URL session
|
2023-07-25 15:21:38 +02:00
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def auth_call(
|
2023-07-27 15:53:10 +02:00
|
|
|
session = None,
|
|
|
|
authtype: str = "basic",
|
|
|
|
user: str = "",
|
|
|
|
passw: str = ""
|
2023-07-25 15:21:38 +02:00
|
|
|
):
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
|
|
|
Authentication for url request. Requires request.sessions.Session() object.
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
- session (object): request Session() object.
|
|
|
|
- authtype (str): 'ntlm', 'basic' or 'digest'.
|
|
|
|
- user (str): auth user.
|
|
|
|
- passw (str): auth password.
|
|
|
|
"""
|
2023-07-27 15:53:10 +02:00
|
|
|
if session is not None:
|
|
|
|
if authtype == 'ntlm':
|
|
|
|
session.auth = HttpNtlmAuth(user, passw)
|
|
|
|
elif authtype == 'basic':
|
|
|
|
session.auth = HTTPBasicAuth(user, passw)
|
|
|
|
elif authtype == 'digest':
|
|
|
|
session.auth = HTTPDigestAuth(user, passw)
|
|
|
|
|
|
|
|
####
|
|
|
|
# Call URL and return output
|
|
|
|
#########################################################################################
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
def call_url(
|
2023-07-27 15:53:10 +02:00
|
|
|
url: str = "",
|
|
|
|
authtype: str = "basic",
|
|
|
|
user: str = "",
|
|
|
|
passw: str = "",
|
|
|
|
timeout: int = 1
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Call URL. Uses request module to get url contents.
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
- url (str): URL
|
|
|
|
- authtype (str): ntlm', 'basic', 'digest'. Optional.
|
|
|
|
- user (str): auth user. Optional.
|
|
|
|
- passw (str): auth password. Optional.
|
2023-07-27 15:53:10 +02:00
|
|
|
- timeout (int): session timeout seconds. Optional.
|
2023-07-25 15:21:38 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
- str: call output
|
|
|
|
"""
|
|
|
|
# using with so we make sure the session is closed even when exceptions are encountered
|
|
|
|
with Session() as session:
|
|
|
|
if authtype != None:
|
|
|
|
auth_call(session, authtype, user, passw)
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
output = ""
|
|
|
|
|
2023-07-25 15:21:38 +02:00
|
|
|
try:
|
2023-07-27 15:53:10 +02:00
|
|
|
output = session.get(url, timeout=timeout, verify=False)
|
2023-07-25 15:21:38 +02:00
|
|
|
except ValueError:
|
2023-07-27 15:53:10 +02:00
|
|
|
output = "Error: URL format not valid (example http://myserver/page.php)"
|
2023-07-25 15:21:38 +02:00
|
|
|
except Exception as e:
|
2023-07-27 15:53:10 +02:00
|
|
|
output = f"{type(e).__name__}:\t{str(e)}"
|
|
|
|
|
|
|
|
return output
|