-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
44 lines (37 loc) · 1.25 KB
/
server.py
File metadata and controls
44 lines (37 loc) · 1.25 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
import http.server
import socketserver
from postgres import fetch_data_from_postgres
# Create an HTML page template
html_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Technical assignment</title>
</head>
<body>
<h1>Technical Assignment - Aikaterini Adam</h1>
<h2>Sell flats from sreality.cz</h2>
</body>
</html>
"""
# Define a custom request handler to serve the HTML page
class CustomRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
data = fetch_data_from_postgres()
html = html_template.replace("{% for title, image_url in data %}", "")
for prop in data:
index = prop['index']
title = prop['title']
link = prop['link']
html += f'<div><h3>{index} - {title}</h3><img src="{link}"</div>'
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(html.encode())
else:
super().do_GET()
with socketserver.TCPServer(("", 8080), CustomRequestHandler) as httpd:
print("Server started on port 8080")
httpd.serve_forever()