From fd1fbcecb1ef75fe8016ab909ae4092ca705b2f1 Mon Sep 17 00:00:00 2001 From: cathook Date: Sun, 9 Nov 2014 02:40:08 +0800 Subject: big change --- test_tool/ping_server.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 test_tool/ping_server.py (limited to 'test_tool/ping_server.py') diff --git a/test_tool/ping_server.py b/test_tool/ping_server.py new file mode 100755 index 0000000..5cdf41b --- /dev/null +++ b/test_tool/ping_server.py @@ -0,0 +1,62 @@ +#! /usr/bin/env python3 + +import log +import sys +import tcp_server +import threading + +from json_package import JSONPackage + +class _TCPConnectionHandler(threading.Thread): + """A thread to handle a connection. + + Attributes: + _sock: The connection socket. + _users_text_manager: An instance of UsersTextManager. + """ + def __init__(self, sock, users_text_manager): + """Constructor. + + Args: + sock: The connection socket. + users_text_manager: An instance of UsersTextManager. + """ + super(_TCPConnectionHandler, self).__init__() + self._sock = sock + self._users_text_manager = users_text_manager + + def run(self): + """Runs the thread.""" + request = self._receive() + self._handle(request) + self._sock.close() + + + def _handle(self, request): + """Handles the request. + + Args: + request: The request package. + """ + log.info(str(request.content) + '\n') + request.send_to(self._sock) + + def _receive(self): + """Receive a request. + + Return: + The request package. + """ + request = JSONPackage() + request.recv_from(self._sock) + return request + + +tcp_server._TCPConnectionHandler = _TCPConnectionHandler + +def main(): + server = tcp_server.TCPServer(int(sys.argv[1]), None) + server.start() + +if __name__ == '__main__': + main() -- cgit v1.2.3