A client written in Python sends commands to the server to execute the corresponding commands and returns the results. it is idle. you can use python to write a program that responds from the client and the server, the main principle is that the client communicates with the server through the tcp protocol. the client sends commands to the server. after the server executes the commands, the corresponding results are returned to the client. the client prints the results and the code is relatively simple, not detailed. Pure entertainment.
Server code, server_tcp.py
#! /Usr/bin/env python #-*-coding: UTF-8-*-# execute the command sent from the client and return the execution result to the 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 () failed t 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) # execute the command handler = subprocess passed by the client. 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 T Exception, e: traceback. print_exc () try: client_socket.close () failed t Exception, e: traceback. print_exc ()
2. client code client_tcp.py
#! /Usr/bin/env python #-*-coding: UTF-8-*-# send the command import socket, sys, traceback host = '2017. 0.0.1 'Port = 51888 s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) try: s. connect (host, port) failed t Exception, e: msg = traceback. format_exc () print 'connection error: ', msg input_command = raw_input ('input command:') s. send (input_command) # use the shutdown () function to change socket bidirectional data transmission to unidirectional data transmission # this parameter indicates how to disable socket. Specifically: 0 indicates that future reading is prohibited; 1 indicates that future writing is prohibited; 2 indicates that future reading and writing are prohibited. shutdown (1) print 'sending completed. 'print 'received content: \ n' while 1: buff = s. recv (4096) if not len (buff): break sys. stdout. write (buff)
3. start the server_tcp.py script and start listening to Port 51888 of the local machine. Then, start client_tcp.py.
(1) client content:
/Usr/bin/python2.7/home/wuguowei/PycharmProjects/xplan_script/test_process/client_tcp.pyInput command: ls-l sent successfully. received content: total usage 20-rw-r -- 1 root 744 February 10 14:44 client_tcp.py-rw-r -- 1 root 877 February 10 14:18 my_sub_process.py-rw-r -- 1 root root 1290 February 10 14:45 server_tcp.py-rw-r -- 1 root 493 February 10 10:43 tcpclient. py-rw-r -- 1 root 1168 February 10 11:51 tcpserver. py okProcess finished with exit code 0
(2) server 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