Coverage for an_website / whats_my_ip / ip.py: 89.474%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-01 02: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/>. 

13 

14"""A page that tells users their IP address.""" 

15 

16 

17from ipaddress import ip_address 

18 

19from ..utils.request_handler import APIRequestHandler, HTMLRequestHandler 

20from ..utils.utils import ModuleInfo 

21 

22 

23def get_module_info() -> ModuleInfo: 

24 """Create and return the ModuleInfo for this module.""" 

25 return ModuleInfo( 

26 handlers=( 

27 (r"/ip", IP), 

28 (r"/api/ip", IPAPI), 

29 ), 

30 name="IP-Informationen", 

31 description="Erfahre deine IP-Adresse", 

32 path="/ip", 

33 keywords=( 

34 "IP-Adresse", 

35 "Was ist meine IP?", 

36 ), 

37 ) 

38 

39 

40class IPAPI(APIRequestHandler): 

41 """The request handler for the IP API.""" 

42 

43 async def get(self, *, head: bool = False) -> None: 

44 """Handle GET requests to the IP API.""" 

45 if head: 

46 return 

47 

48 if geoip := await self.geoip(): 

49 return await self.finish_dict( 

50 ip=self.request.remote_ip, 

51 country=geoip.get("country_flag"), 

52 ) 

53 

54 return await self.finish_dict(ip=self.request.remote_ip) 

55 

56 

57class IP(HTMLRequestHandler): 

58 """The request handler for the IP page.""" 

59 

60 async def get(self, *, head: bool = False) -> None: 

61 """Handle GET requests to the IP page.""" 

62 if head: 

63 return 

64 

65 if not self.request.remote_ip: 

66 return await self.render( 

67 "pages/empty.html", 

68 text="IP-Adresse konnte nicht ermittelt werden.", 

69 ) 

70 

71 await self.render( 

72 "pages/ip.html", 

73 ip=self.request.remote_ip, 

74 flag=( 

75 "🔁" 

76 if ip_address(self.request.remote_ip).is_loopback 

77 else (await self.geoip() or {}).get("country_flag") or "❔" 

78 ), 

79 )