Skip to content

Commit 4b94c81

Browse files
committed
proper response
1 parent e89bf3a commit 4b94c81

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

update_ip.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,21 @@ class RequestHandler(socketserver.BaseRequestHandler):
9393
</body>
9494
</html>
9595
"""
96+
html_template_no_change = """
97+
<html>
98+
<head><title>IP Update Trigger</title></head>
99+
<body>
100+
<h1>IP Update Trigger</h1>
101+
<p>No IP update was necessary.</p>
102+
</body>
103+
</html>
104+
"""
96105

97106
def handle(self):
98107
# Simple HTTP response for trigger
99108
request_data = self.request.recv(1024).decode(errors="ignore")
100109

101-
print(self.request)
110+
# print(self.request)
102111
# Parse HTTP request to extract Host and Path
103112
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + RequestHandler.html_template_nok
104113
lines = request_data.split("\r\n")
@@ -125,11 +134,12 @@ def handle(self):
125134
print(f"[info] Incoming request from: {incoming_ip_address}")
126135

127136
if incoming_ip_address != get_rule_value():
128-
print(f"[info] IP address changed: {incoming_ip_address} != {get_rule_value()}")
129137
update_rule(incoming_ip_address)
138+
print(f"[info] IP address changed: {incoming_ip_address} != {get_rule_value()}")
139+
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + RequestHandler.html_template_ok
130140
else:
131141
print(f"[info] IP address unchanged: {incoming_ip_address} == {get_rule_value()}")
132-
response = RequestHandler.html_template_ok
142+
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + RequestHandler.html_template_no_change
133143

134144
self.request.sendall(response.encode())
135145

@@ -138,9 +148,12 @@ def main():
138148
# listen on TRIGGER_WEBSITE_DOMAIN:TRIGGER_WEBSITE_PORT
139149
while True:
140150
# open http server
141-
with socketserver.TCPServer(('0.0.0.0', TRIGGER_WEBSITE_PORT), RequestHandler) as httpd:
142-
print(f"[info] Listening for updates on {TRIGGER_WEBSITE_DOMAIN}:{TRIGGER_WEBSITE_PORT}")
143-
httpd.serve_forever()
151+
try:
152+
with socketserver.TCPServer(('0.0.0.0', TRIGGER_WEBSITE_PORT), RequestHandler) as httpd:
153+
print(f"[info] Listening for updates on {TRIGGER_WEBSITE_DOMAIN}:{TRIGGER_WEBSITE_PORT}")
154+
httpd.serve_forever()
155+
except Exception as e:
156+
print(f"[error] {e}")
144157
else:
145158
while True:
146159
try:

0 commit comments

Comments
 (0)