improvement
This commit is contained in:
2
utilities/__init__.py
Normal file
2
utilities/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# db/__init__.py
|
||||
print("Initializing the db package")
|
BIN
utilities/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
utilities/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
utilities/__pycache__/get_token.cpython-310.pyc
Normal file
BIN
utilities/__pycache__/get_token.cpython-310.pyc
Normal file
Binary file not shown.
43
utilities/get_token.py
Normal file
43
utilities/get_token.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import sys
|
||||
import urllib.request as urllib2
|
||||
import urllib.parse as urlparse
|
||||
import ssl
|
||||
|
||||
BASEURL = 'https://fhem.auwiesen2.de/fhem?'
|
||||
url = BASEURL + 'detail=WEB'
|
||||
|
||||
def get_token(url):
|
||||
# Split the URL to extract the username and password
|
||||
nurl = urlparse.urlsplit(url)
|
||||
username = nurl.username
|
||||
password = nurl.password
|
||||
|
||||
# Reconstruct the URL without the username and password
|
||||
url = url.replace(f"{username}:{password}@", '')
|
||||
url = url.replace(" ", "%20")
|
||||
|
||||
# Create an unverified HTTPS context (not recommended for production)
|
||||
ssl._create_default_https_context = ssl._create_unverified_context
|
||||
|
||||
# Setup HTTP Basic Authentication
|
||||
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
|
||||
password_mgr.add_password(None, url, username, password)
|
||||
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
|
||||
opener = urllib2.build_opener(handler)
|
||||
urllib2.install_opener(opener)
|
||||
|
||||
try:
|
||||
# Send the request and retrieve the response
|
||||
with urllib2.urlopen(url, data=None, timeout=10) as uu:
|
||||
token = uu.read().decode('utf-8')
|
||||
# Extract the CSRF token
|
||||
token = token[token.find('csrf_'):]
|
||||
token = token[:token.find("'")]
|
||||
return token
|
||||
except urllib2.URLError as e:
|
||||
print(f'URLError: {e.reason}')
|
||||
return False
|
||||
|
||||
# Example usage
|
||||
token = get_token(url)
|
||||
print(f"Token: {token}")
|
Reference in New Issue
Block a user