A python-written client sends instructions to the server to execute the corresponding command and returns the result

Source: Internet
Author: User
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.

    1. 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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.