-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
96 lines (82 loc) · 2.89 KB
/
Copy pathclient.py
File metadata and controls
96 lines (82 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
from PyQt6.QtWidgets import (
QApplication,
QWidget,
QMainWindow,
QPushButton,
QLineEdit,
QLabel,
QMessageBox,
)
from PyQt6.QtCore import QUrl
from PyQt6.QtGui import QDesktopServices
import sys
import webbrowser
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Client")
self.setFixedSize(400, 400)
self.label1 = QLabel("Enter your host IP:", self)
# self.label2 = QLabel("Enter API Key:", self)
self.label3 = QLabel("Enter IP to geolocate:", self)
self.text1 = QLineEdit(self)
# self.text2 = QLineEdit(self)
self.text3 = QLineEdit(self)
self.text1.move(10, 30)
# self.text2.move(10, 90)
self.text3.move(10, 150)
# self.label2.move(10, 60)
self.label3.move(10, 120)
self.button = QPushButton("Geolocate", self)
self.button.move(10, 180)
self.button.clicked.connect(self.on_click)
self.button.pressed.connect(self.on_click)
self.show()
def on_click(self):
hostname = self.text1.text()
# api_key = self.text2.text()
ip = self.text3.text()
if hostname == "" or ip == "":
QMessageBox.about(self, "Error", "Please fill all the fields")
else:
res = self.__query(hostname, ip)
if res:
lat, long = res["lat"], res["lon"]
url = f"https://www.openstreetmap.org/?mlat={lat}&mlon={long}#map=12"
QDesktopServices.openUrl(QUrl(url))
self.show()
def __query(self, hostname, ip):
url = f"http://{hostname}/ip/{ip}"
try:
r = requests.get(url, timeout=5)
if r.status_code == requests.codes.OK:
return r.json()
return None
except requests.exceptions.RequestException as e:
QMessageBox.about(self, "Error", f"Connection error: {str(e)}")
return None
def get_location_from_ip(ip_address):
"""Récupère les informations de localisation d'une adresse IP via ip-api.com"""
url = f"http://ip-api.com/json/{ip_address}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data["status"] == "success":
return data
else:
print(f"Erreur: {data.get('message', 'Erreur inconnue')}")
return None
else:
print(f"Erreur HTTP: {response.status_code}")
return None
def open_in_openstreetmap(latitude, longitude):
"""Ouvre les coordonnées dans OpenStreetMap"""
osm_url = f"https://www.openstreetmap.org/?mlat={latitude}&mlon={longitude}&zoom=12"
print(f"URL OpenStreetMap: {osm_url}")
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec())