Nothing to do, use Python to write a client and server-side response program, the main principle is that the client through the TCP protocol and server-side communication, the client sends instructions to the server side, the server executes the instructions after the corresponding results returned to the client, the client printing results, the code is relatively simple, not detailed introduction. Purely entertaining.
Server-side code, server_tcp.py
#!/usr/bin/env python#-*-coding:utf-8-*-# #执行客户端发送过来的命令, and return execution results to client import socket, traceback, subprocess host = ' Port ' = 51888 s = socket.socket (socket.af_inet, socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1) s.bind ((host, Port)) S.listen (1) while 1:try:client_socket, client_addr = s.accept () except Exception, E:traceback.print_exc () Continue Try:print ' from host: ', Client_socket.getpeername ( ) While 1:command = CLIENT_SOCKET.RECV (4096) if not len (command): Break Print client_socket.getpeername () [0] + ': ' + str (command) # Executes the commands passed by the client handler = subprocess . Popen (Command, Shell=true, stdout=subprocess. PIPE) output = Handler.stdout.readlines () if output is None:output = [] For One_line in Output:client_socket.sendall (one_line) client_socket.sendall ("\ n") Client_socket.sendall ("OK") except Exception, E:traceback.print_exc () Try:client_socket.clos E () except Exception, E:traceback.print_exc ()
2. Client code client_tcp.py
#!/usr/bin/env python#-*-coding:utf-8-*-# #给server端发送命令import socket, sys, traceback host = ' 127.0.0.1 ' port = 51888 s = Socket.socket (socket.af_inet, socket. SOCK_STREAM) Try: S.connect ((host, port)) except Exception, E: msg = TRACEBACK.FORMAT_EXC () print ' connection error: ', msg Input_command = raw_input (' Input command: ') s.send (Input_command) # uses the shutdown () function to make the two-way data transfer of the socket into one-way data transfer # This parameter indicates how to close the socket. In particular, 0 means that future reading is forbidden, 1 is forbidden to write in the future, 2 is forbidden to read and write in the future S.shutdown (1) print ' send complete. ' print ' Received: \ n ' while 1: buff = S.RECV (4096) if not Len (Buff): break sys.stdout.write (Buff)
3. Start the server_tcp.py script, start listening to the native 51888 port, and then start the client_tcp.py.
(1) Client content:
/usr/bin/python2.7/home/wuguowei/pycharmprojects/xplan_script/test_process/client_tcp.pyinput Command:ls- L send complete. Content Received: Total usage 20-rw-r--r--1 root root 744 February 14:44 client_tcp.py-rw-r--r--1 root root 877 February 10 1 4:18 my_sub_process.py-rw-r--r--1 root root 1290 February 14:45 server_tcp.py-rw-r--r--1 root root 493 February 10 10:43 tcpclient.py-rw-r--r--1 root root 1168 February 11:51 tcpserver.py okprocess finished with exit code 0
(2) server-side information
/usr/bin/python2.7/home/wuguowei/pycharmprojects/xplan_script/test_process/server_tcp.pyfrom Host: (' 127.0.0.1 ', 46993) 127.0.0.1:ls-l