# Py - TCP/IP time server
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-10-29  initial release

# Linux   client: $ nc  localhost 5000
# Windows client: Putty | Host name = 192.168.0.16 | Port = 5000 |
#				  Connection type = other | Raw | Open
# Android client: TCP Client from Sollae Systems

import	time
import  socket

# ============================ local =============================

# get time as string
def getTime():
    t = time.localtime()
    return time.strftime('%H:%M:%S', t)

# ============================ main ==============================

HOST	= '0.0.0.0'			# all interfaces
PORT	= 5000           	# listening port

def main():
    print('server running ...')
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ss:	# IPv4 & stream
        ss.setsockopt(socket.SOL_SOCKET ,socket.SO_REUSEADDR, 1)
        ss.bind((HOST, PORT))										# bind unbound address									
        ss.listen(0)												# enable server, allowed unaccepted connections
        while True:
            cs, addr = ss.accept()										# client socket object
            with cs:
                print('Connected by', addr)
                data = cs.recv(1024)
                if data:
                    print('Rx:', data, '\n')
                    tx_data = f'HTTP/1.0 200 OK\n\n{getTime()}\n'
                    cs.sendall(tx_data.encode())
#            cs.close()

main()

# ============================ end ===============================
