solar-display/server/main.py

31 lines
949 B
Python
Raw Normal View History

2021-06-22 04:05:20 +00:00
import asyncio
from datetime import datetime, timedelta
async def proxy(reader, writer):
message = await reader.read(1024)
addr = writer.get_extra_info('peername')
try:
print('Recvd from {}: {}'.format(addr[0], message))
offset = 32
date_time_obj = datetime.strptime(str(message)[offset:offset+14], '%Y%m%d%H%M%S') + timedelta(minutes=-5)
send_str = '101' + datetime.strftime(date_time_obj, '%Y%m%d%H%M%S')
send_data = send_str.encode()
print('Sending to {}: {}'.format(addr[0], send_data))
writer.write(send_data)
await writer.drain()
except ValueError:
print('Ignored unnecessary data')
writer.close()
async def main():
for port in [8995, 8996, 8997]:
server = await asyncio.start_server(proxy, '192.168.69.69', port)
task = asyncio.create_task(server.serve_forever())
# block here for now
await task
asyncio.run(main())