socket_protocol/_examples_/socket_protocol_server.py

31 lines
785 B
Python
Raw Normal View History

2021-01-11 17:46:05 +01:00
import sys
sys.path.append('../..')
import report
import socket_protocol
import tcp_socket
import time
def time_callback(msg):
if msg.get_status() == socket_protocol.STATUS_OKAY:
return socket_protocol.STATUS_OKAY, time.asctime()
else:
return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
sp = socket_protocol.pure_json_protocol(s, channel_name='example_server')
sp.register_callback(socket_protocol.SID_READ_REQUEST, 0, time_callback)
i = 0
while not s.is_connected() and i <= 20:
i += 1
time.sleep(.1) # wait for a connection
i = 0
while s.is_connected() and i <= 20:
i += 1
time.sleep(.1) # wait for disconnect