Python Library Socket Protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

socket_protocol_server.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import sys
  2. sys.path.append('../..')
  3. import report
  4. import socket_protocol
  5. import tcp_socket
  6. import time
  7. DID_ASC_TIME = 0
  8. class example_protocol(socket_protocol.pure_json_protocol):
  9. def __init__(self, *args, **kwargs):
  10. socket_protocol.pure_json_protocol.__init__(self, *args, **kwargs)
  11. #
  12. self.add_data((socket_protocol.SID_READ_REQUEST, socket_protocol.SID_READ_RESPONSE), DID_ASC_TIME, 'asc_time')
  13. #
  14. if not self.__comm_inst__.IS_CLIENT:
  15. self.register_callback(socket_protocol.SID_READ_REQUEST, 0, self.time_callback)
  16. def time_callback(self, msg):
  17. if msg.get_status() == socket_protocol.STATUS_OKAY:
  18. return socket_protocol.STATUS_OKAY, time.asctime()
  19. else:
  20. return socket_protocol.STATUS_OPERATION_NOT_PERMITTED, None
  21. if __name__ == '__main__':
  22. report.stdoutLoggingConfigure(log_name_lvl=[('root', 'INFO'), ])
  23. s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
  24. sp = example_protocol(s, channel_name='example_server')
  25. i = 0
  26. while not s.is_connected() and i <= 20:
  27. i += 1
  28. time.sleep(.1) # wait for a connection
  29. i = 0
  30. while s.is_connected() and i <= 20:
  31. i += 1
  32. time.sleep(.1) # wait for disconnect