-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
114 lines (104 loc) · 3.81 KB
/
server.py
File metadata and controls
114 lines (104 loc) · 3.81 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from evento_online import EventoOnline
ev1 = EventoOnline("Live de Python")
ev2 = EventoOnline("Live de Javascript")
ev3 = EventoOnline("Live dos Paranauê")
eventos = [ev1, ev2, ev3]
class SimpleHandler(BaseHTTPRequestHandler):
def do_GET(self):
print('👻')
if self.path == '/':
data = f"""
<html>
<head>
<title>😀</title>
</head>
<body>
<section>
<div>
😀 Olá from {self.path}
</div>
</section>
</body>
<html>
""".encode()
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('X-TESTE', 'teste')
self.end_headers()
self.wfile.write(data)
elif self.path == '/eventos':
css = """
<style>
table {
border-collapse: collapse;
}
table th:first-of-type {
width: 10%;
}
table th:nth-of-type(2) {
width: 10%;
}
table th:nth-of-type(3) {
width: 50%;
}
table th:nth-of-type(4) {
width: 30%;
}
td {
text-align: center;
border: 1px solid #3e3e3e40
}
</style>
"""
tbody_content_eventos = ''
for ev in eventos:
tbody_content_eventos += f"""
<tr>
<td>{ev.id}</td><td>{ev.nome}</td><td>{ev.local}</td>
</tr>\n
"""
data = f"""
<html>
<head>
<title>Eventos</title>
</head>
<body>
{css}
<section>
<div style="display:flex; flex-direction: column; align-items:center; justify-content:center">
<table>
<thead>
<tr>
<th>ID</th><th>Nome</th><th>Link</th>
</tr>
</thead>
<tbody>
{tbody_content_eventos}
</tbody>
</table>
</div>
</section>
</body>
<html>
""".encode()
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.end_headers()
self.wfile.write(data)
elif self.path == '/api/eventos':
lista_dict_eventos = []
for ev in eventos:
lista_dict_eventos.append({
"id": ev.id,
"nome": ev.nome,
"local": ev.local
})
data = json.dumps(lista_dict_eventos).encode()
self.send_response(200)
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(data)
server = HTTPServer(('localhost', 8000), SimpleHandler)
server.serve_forever()