-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
30 lines (23 loc) · 773 Bytes
/
Copy pathserver.py
File metadata and controls
30 lines (23 loc) · 773 Bytes
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
import socket
import datetime
class Server:
def __init__(self):
self.port = 123
self.address = "127.0.0.1"
self.offset = self.read_offset()
def get_time_with_offset(self):
time = datetime.datetime.now()
return time + datetime.timedelta(seconds=self.offset)
def listen(self):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as so:
so.bind((self.address, self.port))
while True:
data, sender = so.recvfrom(254)
so.sendto(str(self.get_time_with_offset()).encode("utf-8"), sender)
@staticmethod
def read_offset():
with open("conf.txt", "r") as f:
line = f.readline()
return int(line)
s = Server()
s.listen()