Coverage for an_website/whats_my_ip/ip.py: 90.000%
20 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-16 19:56 +0000
1# This program is free software: you can redistribute it and/or modify
2# it under the terms of the GNU Affero General Public License as
3# published by the Free Software Foundation, either version 3 of the
4# License, or (at your option) any later version.
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU Affero General Public License for more details.
10#
11# You should have received a copy of the GNU Affero General Public License
12# along with this program. If not, see <https://www.gnu.org/licenses/>.
14"""A page that tells users their IP address."""
16from __future__ import annotations
18from ipaddress import ip_address
20from ..utils.request_handler import APIRequestHandler, HTMLRequestHandler
21from ..utils.utils import ModuleInfo
24def get_module_info() -> ModuleInfo:
25 """Create and return the ModuleInfo for this module."""
26 return ModuleInfo(
27 handlers=(
28 (r"/ip", IP),
29 (r"/api/ip", IPAPI),
30 ),
31 name="IP-Informationen",
32 description="Erfahre deine IP-Adresse",
33 path="/ip",
34 keywords=(
35 "IP-Adresse",
36 "Was ist meine IP?",
37 ),
38 )
41class IPAPI(APIRequestHandler):
42 """The request handler for the IP API."""
44 async def get(self, *, head: bool = False) -> None:
45 """Handle GET requests to the IP API."""
46 if head:
47 return
49 if geoip := await self.geoip():
50 return await self.finish_dict(
51 ip=self.request.remote_ip,
52 country=geoip.get("country_flag"),
53 )
55 return await self.finish_dict(ip=self.request.remote_ip)
58class IP(HTMLRequestHandler):
59 """The request handler for the IP page."""
61 async def get(self, *, head: bool = False) -> None:
62 """Handle GET requests to the IP page."""
63 if head:
64 return
66 if not self.request.remote_ip:
67 return await self.render(
68 "pages/empty.html",
69 text="IP-Adresse konnte nicht ermittelt werden.",
70 )
72 await self.render(
73 "pages/ip.html",
74 ip=self.request.remote_ip,
75 flag=(
76 "🔁"
77 if ip_address(self.request.remote_ip).is_loopback
78 else (await self.geoip() or {}).get("country_flag") or "❔"
79 ),
80 )