Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error due to IP address switch #92

Open
Gezzo42 opened this issue Jan 8, 2024 · 3 comments
Open

Error due to IP address switch #92

Gezzo42 opened this issue Jan 8, 2024 · 3 comments

Comments

@Gezzo42
Copy link
Contributor

Gezzo42 commented Jan 8, 2024

I recently changed my Homeassistant hardware. Afterwards I was not able to use the FRITZ!SmartHome integration. I got the following error:
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: http://fritz.box/webservices/homeautoswitch.lua?switchcmd=gettemplatelistinfos&sid=88a3e4bb76d62fe0
The Fritz!Box reports an failed log in. Tests with the CLI results in the same error, but not on my normal PC. It seems to be similar to the problem of the following report.
Further investigation using wireshard revealed the problem:
grafik
The used IPv6 address changed between the different request and the address needs to be the same, otherwise the request failes.
My first idea was to get the source IP address of the first request and set it for further connections. But getting the source address is tricky and did not work with the Fritz!Box (further information).
I came up with the following quick and dirty solution in fritzhome.py:

import requests
requests.packages.urllib3.util.connection.HAS_IPV6 = False

Do you have an idea for a better solution? Especially for integration into pyfritzhome.

Some background on the environment: Raspberry Pi OS Lite 64 based on Debian 12 with the IP addresses assigned via DHCP.

@Gezzo42 Gezzo42 changed the title Log in error due to IP address switch Error due to IP address switch Jan 8, 2024
@mib1185
Copy link
Collaborator

mib1185 commented Jan 15, 2024

there is a known bug with IPv6 and link local address in the pythons request module (python/cpython#73561). it should be fixed in upcoming release of python 3.11 and 3.12

@Gezzo42
Copy link
Contributor Author

Gezzo42 commented Jan 15, 2024

Thanks for the link. I will test it.

@Gezzo42
Copy link
Contributor Author

Gezzo42 commented Feb 6, 2024

there is a known bug with IPv6 and link local address in the pythons request module (python/cpython#73561). it should be fixed in upcoming release of python 3.11 and 3.12

I compiled Python 3.12.1, which should have the bugfix for the mentioned bug. Then I ran python-fritzhome from a virtual environment, but got the same error:

(venv-p3.12) user@host:~ $ fritzhome -v -f fritz.box -u user -p password list
INFO:pyfritzhome.fritzhome:Updating Devices ...
DEBUG:pyfritzhome.fritzhome:logout
Traceback (most recent call last):
  File "/home/user/venv-p3.12/bin/fritzhome", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/cli.py", line 329, in main
    args.func(fritzbox, args)
  File "/home/user/python-fritzhome/pyfritzhome/cli.py", line 21, in list_all
    devices = fritz.get_devices()
              ^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 162, in get_devices
    return list(self.get_devices_as_dict().values())
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 167, in get_devices_as_dict
    self.update_devices()
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 129, in update_devices
    for element in self.get_device_elements():
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 150, in get_device_elements
    return self._get_listinfo_elements("device")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 143, in _get_listinfo_elements
    plain = self._aha_request("get" + entity_type + "listinfos")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 85, in _aha_request
    plain = self._request(url, params)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/python-fritzhome/pyfritzhome/fritzhome.py", line 42, in _request
    rsp.raise_for_status()
  File "/home/user/venv-p3.12/lib/python3.12/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: http://fritz.box/webservices/homeautoswitch.lua?switchcmd=getdevicelistinfos&sid=<sid>

I tried both IPv6 addresses, which I got for the DNS name fritz.box, and it worked. But the IPv6 addresses did not work with python-fritzhome and the Python version 3.11.2. This problem seems to be fixed by the mentioned bug.

I think, it is a problem in the underlying libraries (requests, urllib3 or httplib).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants