def _send_command(self, command): HOST = "localhost" PORT = 50007 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) sock.settimeout(1.0) bytes = prepare_data_for_sending(command) sock.sendall(bytes) data = recieve_data_from_socket(sock) sock.close() return data
def _send_command(self, command): HOST = 'localhost' PORT = 50007 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) sock.settimeout(1.0) bytes = prepare_data_for_sending(command) sock.sendall(bytes) data = recieve_data_from_socket(sock) sock.close() return data
def _socket_handler(self, conn, addr): conn.settimeout(1.0) while not self.do_stop: try: recieved_bytes = recieve_data_from_socket(conn) command, data = parse_recieved_bytes(recieved_bytes) logging.info("Command=%s, data=%s", command, data) except socket.timeout: continue except OSError as msg: logging.error("OSError: %s", msg) self.stop_server() if command == 'connect': self._send_data_to_socket(conn, 'connected') elif command == 'ping': self._send_data_to_socket(conn, 'pong') elif command == 'pingd': self._send_data_to_socket(conn, 'pongd ' + data) elif command == 'quit': if data: self._send_data_to_socket(conn, 'ackquit ' + data) else: self._send_data_to_socket(conn, 'ackquit') conn.close() break elif command == 'finish': self._send_data_to_socket(conn, 'ackfinish') conn.close() self.do_stop = True break elif command: self._send_data_to_socket(conn, 'invalid command') else: self._send_data_to_socket(conn, 'ackfinish') conn.close() self.threads.remove(threading.currentThread()) logging.info("Thread off conn=%s", conn)
import socket from work.cmdargs import parse_arguments from work.general import recieve_data_from_socket from work.utils import prepare_data_for_sending, parse_recieved_bytes if __name__ == "__main__": args = parse_arguments() HOST = args.host PORT = args.port sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1.0) sock.connect((HOST, PORT)) sock.sendall(prepare_data_for_sending('connect')) data = sock.recv(1024) print('<<< ', data) while True: user_input = input('>>> ') sock.sendall(prepare_data_for_sending(user_input)) data = recieve_data_from_socket(sock) print('<<< ', data) command, _ = parse_recieved_bytes(data) if command in ['ackquit', 'ackfinish']: break sock.close()
import socket from work.cmdargs import parse_arguments from work.general import recieve_data_from_socket from work.utils import prepare_data_for_sending, parse_recieved_bytes if __name__ == "__main__": args = parse_arguments() HOST = args.host PORT = args.port sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1.0) sock.connect((HOST, PORT)) sock.sendall(prepare_data_for_sending('connect')) data = sock.recv(1024) print ('<<< ', data) while True: user_input = input('>>> ') sock.sendall(prepare_data_for_sending(user_input)) data = recieve_data_from_socket(sock) print ('<<< ', data) command, _ = parse_recieved_bytes(data) if command in ['ackquit', 'ackfinish']: break sock.close()