python network programming exercises

1. What is the C / S structure?

  C refers to a client (client software), S refers Server (server software) 
  a C / S architecture is to achieve the server software and client software based on network traffic. 

Internet everywhere is C / S structure 
  , such as 12306 is the server, your browser is the client (B S architecture is C / one kind / S architecture) 
  Tencent as a server to provide the video for you, you have the next Tencent video client to see its video) 
 
C relations / S architecture and the socket: 
  we learn socket is to complete the C / S structure development

2 What internet protocols are? They introduced five features in each layer of the protocol?

English has become the standard for all unified communications world, communication between the computer should have one like English as a communication standard, which is called Internet Protocol, it can be very clear: Internet Protocol is the computer industry's English network is the physical link media + Internet protocol. 
We need to do is to let the world learn computers Internet protocol, so any computer in the message are in strict accordance with the format specified in the agreement to organize the data, the recipient can parse out the results according to the same protocol, and this to achieve a computer accessible around the world can communicate.
According to different functions, people will be divided into Internet protocol osi seven or tcp
/ ip five or tcp / ip four (we only need to have tcp / ip protocol to five), this stratification is like learning English several stages, each stage should have specialized skills or complete a specific task, such as: 1, learn phonetics 2, learn the words 3, 4 grammar school, writing.

3, tcp protocol-based communications, why the need for three-way handshake to establish a link, and unlink it takes four to wave

Why three times waving? 
 
  In only two "handshake" scenario, assume that Server Client wanted to establish a connection, but because of the way the connection request datagram is lost, so the Client end had to send it again; only this time Server client receives a connection request , it can be normal to establish a connection. However, sometimes the Client-side resend requests are not reported because the data is lost, but it is possible because of data transmission in a large amount of concurrent network nodes are blocked, this situation will end Server has received 2 requests and continue to wait for two Client requests to send data to him ... the problem here, Cient end actually only one request, and the Server side there are two responses extreme cases may be due to the Client-side several times to re-send the requested data Server-side which led to the establishment of a final response waiting more than N, resulting in a great waste of resources! Therefore, the "three-way handshake" is necessary! Why four wave? 
  Imagine now if you are a client you want to disconnect all connections with the Server how to do? The first step, you stop sending data to the Server side, and wait for a reply Server. But things are not yet finished, though you do not own Server to send data, but because before you have established a good connection equal, so this time he also had the initiative to send data to you; it had to end terminate Server sends to you data, and wait for your confirmation. In fact, it plainly is to ensure the full implementation of a contract between the two sides!
 

 

  Three-way handshake: client sends a request to establish channels; server receives the request and agreed, but also sends a request to build channels; client receives the request and agreed to establish complete

  Four wave: client request to disconnect the transmission path; Server receives the request and agrees also reply to a message on the client; server also sends disconnection request channel; client by End of Message

Why TCP protocol termination links to four times? 
 
1, when the host A and the transmission data confirmation Ends know B has finished receiving, want to close the port to send data (or send the acknowledgment signal may, of course), the host will send FIN B. 
 
2, FIN A host B receives the transmitted, acknowledged receipt, sends ACK reply. 
 
3, but it is still possible to send data B, do not want to close the mouth of the meaning of the data, the FIN and ACK is not transmitted simultaneously, but wait until the data is sent over B, FIN will be sent to the host A. 
 
4, A receives the FIN sent to B, B to know also transmitted over the data, ACK reply, A after waiting 2MSL, B did not receive any message came to know their B has received the ACK, A is closed link, B also closed links.

4. Why tcp protocol-based communication is more reliable than the udp-based communication protocol?

tcp: a reliable partner to acknowledge receipt of the message, next only made one, if it did not receive a confirmation message retransmission
udp; no reliable data has been sent, no other response

(1) UDP: user datagram protocol ( User Datagram Protocol) 
  Features: 
  --1: connectionless-oriented: the source and destination need not establish the connection before transmitting data 
  --2: the size of each data packet are limited to 64k (8 bytes) within 
  --3: for unreliable protocol packets (i.e., data is not necessarily sent out is received) 
  --4: transmission rate, high efficiency 
  --5: real-life examples: the post office member, real-time online chat, video protocol, etc. 
(2) TCP: transmission control protocol ( transmission control protocol) 
  features: 
  --1: connection-oriented: the need to establish a data connection prior to transmitting 
  --2: a lot of data during a connection transmission 
  --3: complete the connection through the "three-way handshake" in a way that is safe and reliable protocol 
  --4: transmission efficiency is low, slow

5, streaming protocol refers to any agreement, Datagram Protocol refers to any agreement?

TCP protocol, reliable transmission 
Datagram Protocol: UDP protocol, not transport 
is the difference between UDP and TCP: 
    TCP is connection-oriented, reliable byte stream service 
    UDP datagram oriented services connectionless

6. What is the socket? DESCRIPTION socket communication protocol is based on the process flow of formula tcp protocol:

Socket application layer and the intermediate software TCP / IP protocol suite to communicate abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol suite is hidden behind the Socket interface for the user, a set of simple interface is all. 

  Server: Create a socket object, bind ip port bind (), set the maximum number of links listen (), accept () with the client connect () to create a two-way conduit, send (), recv () , close () 

  Client: create a socket object, connect () with the server accept () to create a two-way conduit, send (), recv () , close ()

7. What is the stick package? What are the reasons socket caused stick package is? Stick package which case the phenomenon occur?

Stick package: data stick together, mainly because of: the recipient does not know the limits between the message, do not know how many bytes smaller time extraction of data caused by the amount of data, the time interval is short, it is merged into a package, this is a low-level optimization algorithm (Nagle algorithm)

8, the development of a socket-based chat program, to realize sending and receiving messages to each other at both ends

  Server:

Coding _ * _ #: _ * _. 8 UTF- 
#. 8, a chat program Socket develop, implement message transmitting and receiving ends to each other based on 
Import Socket 
for ip_port = ( '127.0.0.1', 8088) 
Link = socket.socket (Socket .AF_INET, socket.SOCK_STREAM) 
link.bind (ip_port) 
link.listen (5) 
 
Print ( "waiting for data connection: .." "") 
after # blocked until there is connection, with a new connection comes in, as it will the request generating a connection object 
Conn, addr = link.accept () 
 
client_data = conn.recv (1024) 
Print ( "this message is received:", client_data.decode ( 'UTF-. 8')) 
conn.send ( client_data.upper ()) 
 
conn.Close () 
link.close ()

  Client:

Coding _ * _ #: _ * _. 8 UTF- 
#. 8, a socket-based chat program to develop, implement sending and receiving messages to each other at both ends / 
Import socket 
for ip_port = ( '127.0.0.1', 8088) 
Link socket.socket = ( socket.AF_INET, socket.SOCK_STREAM) 
link.connect (for ip_port) 
 
Print ( "start sending data") 
cmd = iNPUT ( "enter: >>.") Strip () 
link.send (cmd.encode ( 'UTF-. 8 ')) 
Data = link.recv (1028) 
Print (Data) 
 
link.close ()

9, based on the implementation of tcp socket, simple to develop a remote command that allows users to execute commands, and returns the result

  Server:

Coding _ * _ #: _ * _. 8 UTF- 
#. 9, based on tcp socket, to develop a simple remote command execution program, allowing the user to execute the command, and returns the result 
Import Socket 
Import struct 
Import The subprocess 
 
for ip_port = ( '127.0.0.1', 9999) 
SK = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
sk.bind (for ip_port) 
sk.listen (. 5) 
the while True: # coupling cycles 
    Conn, addr = sk.accept () 
    Print (Conn, addr) 
    the while true: # communications cycle 
        client_data = conn.recv (1024) 
        # processing 
        RES = subprocess.Popen (client_data.decode ( 'UTF-. 8'), the shell = true, 
                               stdout = subprocess.PIPE, 
                               stderr = subprocess.PIPE) 
        stdout = res.stdout.read ()
        = res.stderr.read stderr () 
        # first transmitter head (turn into bytes type of fixed length, then how it turn? to use the struct module) 
        length = len (stdout) + len (stderr) 
        header = struct.pack ( 'I', length) 
        conn.send (header) 
        conn.send (stderr) 
        conn.send (stdout) 
    conn.Close () 
sk.close () 
 
conn.Close () 
sk.close ()

  Client:

Coding _ * _ #: _ * _. 8 UTF- 
#. 9, based on tcp socket, to develop a simple remote command execution program, allowing the user to execute the command, and returns the result 
Import Socket 
Import struct 
 
for ip_port = ( '127.0.0.1', 9999) 
socket.socket = SK (socket.AF_INET, socket.SOCK_STREAM) 
sk.connect (ip_port) 
the while True: 
    cmd = the iNPUT ( "Please enter the command: >>"). Strip () 
    IF not cmd: the Continue 
    sk.send (cmd .encode ( 'UTF-. 8')) 
    header_struct = sk.recv (1024) 
    unpack_res struct.unpack = ( 'I', header_struct) 
    total_size unpack_res = [0] 
    recv_size = 0 
    total_data = B '' 
    the while recv_size <total_size: 
        recv_data = sk.recv(1024)
        recv_size += len(recv_data)
        total_data += recv_data
    print ( "message is returned:% S"% total_data.decode ( 'GBK')) 
sk.close ()

10, written in simple protocol based on tcp FTP program to upload, download files function, and stick package to solve the problem

  Client:

Coding _ * _ #: _ * _. 8 UTF- 
 
Import Socket 
Import struct 
Import JSON 
downlaod_dir = r'D: \ File Transfer \ Client \ downloads' 
 
for ip_port = ( '127.0.0.1', 8808) 
Phone = socket.socket (Socket .AF_INET, socket.SOCK_STREAM) 
phone.connect (ip_port) 
the while True: 
    cmd = the INPUT ( ">>>") Strip () #get D:. \ file transfer \ Server \ a.avi 
    IF not cmd: the Continue 
    Phone. the send (cmd.encode ( 'UTF-8')) 
    # accept the contents of the file to write way to open a new file, sent by the server to accept the contents of the file, and writes the client a new file 
    # first step, close the length of the head, and then unpack 
    obj = phone.recv (1024) 
    header_size struct.unpack = ( 'I', obj) [0] 
    # and then close the second header portion 
    header_bytes = phone.recv (header_size) 
 
    # third portion from header parsing addition to descriptions of real data 
    header_json = header_bytes. decode ( 'utf-8')
    = json.loads header_dic (header_json) 
    '' ' 
     header_dic = { 
                ' filename ': filename, # a.avi 
                ' MD5 ':' dsdsd ', 
                ' FILE_SIZE ': os.path.getsize (filename) 
            }' '' 
    Print ( header_dic) 
    total_size header_dic = [ 'FILE_SIZE'] 
    filename = header_dic [ 'filename'] 
 
    # fourth step of receiving actual data 
    with open ( '% s /% s'% (downlaod_dir, filename), 'wb') as f : 
        recv_size = 0 
        # recv_data = B ''recv_size))
    # print(recv_data.decode('utf-8'))
        while recv_size <total_size:
            res = phone.recv(1024)
            # recv_data += res
            f.write (RES) 
            recv_size + = len (RES) 
            Print ( "total size:% s \ n has Size:% S"% (total_size, recv_size)) 
 
phone.close ()

  Server:

Coding _ * _ #: _ * _. 8 UTF- 
Import The subprocess 
Import Socket 
Import struct 
Import JSON 
Import OS 
share_dir R & lt = '/ file transfer / Server / Share' 
 
Phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 
for ip_port = ( '127.0.0.1', 8808) 
phone.bind (ip_port) 
phone.listen (5) 
Print ( "Starting ....") 
the while True: # cycle link 
    conn, client_addr = phone.accept () 
    Print (client_addr) 
    while True: # communications cycle 
        the try: 
            # command received 
            RES = conn.recv (1024) # b'get a.txt ' 
            IF Not RES: Continue 
            # parse command, extracts the corresponding parameters  
            cmds = res.decode (' utf-8 ') .split ()
            filename = cmds [1] 
            # go open the file for reading, reading the contents of the file to the client 
            # with Open (filename, 'RB') AS F: 
            # conn.s 
              # development of a fixed-length header 
            header_dic = { 
                'filename ': filename, # a.avi 
                ' MD5 ':' dsdsd ', 
                ' FILE_SIZE ': os.path.getsize (R & lt "% S /% S"% (share_dir, filename)) 
            } 
            header_json = json.dumps (header_dic) 
            = header_json.encode header_bytes ( 'UTF-. 8') 
             # first transmission header length 
            conn.send (struct.pack ( 'I', len (header_bytes))) 
             # re-transmitter head 
            conn.the send (header_bytes) 
             # recurrence real data
            with open('%s/%s'%(share_dir,filename),'rb') as f:
                # conn.send(f.read())
                for line in f:
                    conn.send(line)
 
 
        except ConnectionResetError:
            break
    conn.close()
phone.close()

Function version of the FTP program

  The server version of the function:

# _*_ coding: utf-8 _*_
import socket
import os
import struct
import pickle
 
dirname = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(dirname, 'share')
 
def get(cmds,conn):
    filename = cmds[1]
    file_path = os.path.join(filepath, filename)
    if os.path.isfile(file_path):
        header = {
            'filename': filename,
            'md5': 'xxxxxx',
            'file_size': os.path.getsize(file_path)
        }
        header_bytes = pickle.dumps(header)
        conn.send(struct.pack('i', len(header_bytes)))
        conn.send(header_bytes)
 
        with open(file_path, 'rb') as f:
            for line in f:
                conn.send(line)
    else:
        conn.send(struct.pack('i', 0))
 
def put(cmds,conn):
    pass
 
def run():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('127.0.0.1', 8080))
    server.listen(5)
    print('starting...')
    while True:
        conn, client_addr = server.accept()
        print(client_addr)
        while True:
            try:
                res = conn.recv(1024)
                if not res: continue
                cmds = res.decode('utf-8').split()
                if cmds[0] == 'get':
                    get(cmds,conn)
                elif cmds[0] == 'put':
                    put(cmds,conn)
            except ConnectionResetError:
                break
        conn.close()
 
    server.close()
 
if __name__ == '__main__':
    run()

  Function version of the client:

# _*_ coding: utf-8 _*_
import socket
import struct
import pickle
import os
 
dirname = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(dirname,'download')
 
def get(client):
    obj = client.recv(4)
    header_size = struct.unpack('i', obj)[0]
    if header_size == 0:
        print('文件不存在')
    else:
        header_types = client.recv(header_size)
        header_dic = pickle.loads(header_types)
        print(header_dic)
        file_size = header_dic['file_size']
        filename = header_dic['filename']
 
        with open('%s/%s' % (filepath, filename), 'wb') as f:
            recv_size = 0
            while recv_size < file_size:
                res = client.recv(1024)
                f.write(res)
                recv_size += len(res)
                print('总大小:%s 已下载:%s' % (file_size, recv_size))
 
def put():
    pass
 
def run():
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.connect(('127.0.0.1',8080))
    while True:
        msg = input(">>>:").strip()  # get a.txt
        if not msg:continue
        client.send(msg.encode('utf-8'))
 
        cmds = msg.split()
        if cmds[0] == 'get':
            get(client)
        elif cmds[0] == 'put':
            put()
 
    client.close()
 
if __name__ == '__main__':
    run()

Object-oriented FTP program

  Object-oriented version of the server:

# _*_ coding: utf-8 _*_
import socket
import os
import struct
import pickle
 
 
class TCPServer:
    address_family = socket.AF_INET
    socket_type = socket.SOCK_STREAM
    listen_count = 5
    max_recv_bytes = 8192
    coding = 'utf-8'
    allow_reuse_address = False
    # 下载的文件存放路径
    down_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'share')
    # 上传的文件存放路径
    upload_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'upload')
 
    def __init__(self,server_address,bind_and_listen=True):
        self.server_address = server_address
        self.socket = socket.socket(self.address_family,self.socket_type)
 
        if bind_and_listen:
            try:
                self.server_bind()
                self.server_listen()
            except Exception:
                self.server_close()
 
    def server_bind(self):
        if self.allow_reuse_address:
            self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        self.socket.bind(self.server_address)
 
    def server_listen(self):
        self.socket.listen(self.listen_count)
 
    def server_close(self):
        self.socket.close()
 
    def server_accept(self):
        return self.socket.accept()
 
    def conn_close(self,conn):
        conn.close()
 
    def run(self):
        print('starting...')
        while True:
            self.conn,self.client_addr = self.server_accept()
            print(self.client_addr)
            while True:
                try:
                    res = self.conn.recv(self.max_recv_bytes)
                    if not res:continue
                    cmds = res.decode(self.coding).split()
                    if hasattr(self,cmds[0]):
                        func = getattr(self,cmds[0])
                        func(cmds)
                except Exception:
                    break
            self.conn_close (self.conn) 
 
    DEF GET (Self, CMDS): 
        "" "Download 
        1. Locate the downloaded file 
        2. Send header_size 
        3. FILE_SIZE transmission header_bytes 
        4. rb read file transmission Send (Line) 
        5. The If the file does not exists, transmitting Tip 0 client: the file does not exist 
        : param cmds: downloaded file EG: [ 'GET', 'a.txt'] 
        : return: 
        "" " 
        filename CMDS = [. 1] 
        file_path the os.path.join = ( self.down_filepath, filename) 
        IF The os.path.isfile (file_path): 
            header = { 
                'filename': filename, 
                'MD5': 'xxxxxx', 
                'FILE_SIZE': the os.path.getsize(file_path)
            }
            = the pickle.dumps header_bytes (header) 
            self.conn.send (struct.pack ( 'I', len (header_bytes))) 
            self.conn.send (header_bytes) 
            with Open (file_path, 'RB') AS F: 
                for Line F in: 
                    self.conn.send (Line) 
        the else: 
            self.conn.send (struct.pack ( 'I', 0)) 
 
    DEF PUT (Self, CMDS): 
        "" "Upload 
        1. files received four bytes to give the header_size 
        2. the header_dic header_size obtained header_bytes 
        3. header_dic obtained according file_size 
        opening 3. written form file f.write () 
        : param CMDS: downloaded file EG: [ 'PUT', 'a.txt'] 
        : return : 
        """
        obj = self.conn.recv(4)
        header_size = struct.unpack('i', obj)[0]
        header_bytes = self.conn.recv(header_size)
        header_dic = pickle.loads(header_bytes)
        print(header_dic)
        file_size = header_dic['file_size']
        filename = header_dic['filename']
 
        with open('%s/%s' % (self.upload_filepath, filename), 'wb') as f:
            recv_size = 0
            while recv_size < file_size:
                res = self.conn.recv(self.max_recv_bytes)
                f.write(res)
                recv_size += len(res)
 
 
tcp_server = TCPServer(('127.0.0.1',8080))
tcp_server.run()
tcp_server.server_close()

  Object-oriented version of the client:

# _*_ coding: utf-8 _*_
import socket
import struct
import pickle
import os
 
 
class FTPClient:
    address_family = socket.AF_INET
    socket_type = socket.SOCK_STREAM
    # 下载的文件存放路径
    down_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'download')
    # 上传的文件存放路径
    upload_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'share')
    coding = 'utf-8'
    max_recv_bytes = 8192
 
    def __init__(self, server_address, connect=True):
        self.server_address = server_address
        self.socket = socket.socket(self.address_family, self.socket_type)
        if connect:
            try:
                self.client_connect()
            except Exception:
                self.client_close()
 
    def client_connect(self):
        self.socket.connect(self.server_address)
 
    def client_close(self):
        self.socket.close()
 
    def run(self):
        while True:
            # get a.txt 下载   put a.txt 上传
            msg = input(">>>:").strip()
            if not msg: continue
            self.socket.send(msg.encode(self.coding))
            cmds = msg.split()
            if hasattr(self,cmds[0]):
                func = getattr(self,cmds[0])
                func(cmds)
 
    GET DEF (Self, CMDS): 
        "" "Download 
        1 obtained header_size 
        2. header_dic obtained header_types 
        3. file_name obtained FILE_SIZE 
        4. Open the file in the form of written 
        : param cmds: Downloading eg: cmds = [ 'get' , 'a.txt'] 
        : return: 
        "" " 
        obj = self.socket.recv (. 4) 
        header_size struct.unpack = ( 'I', obj) [0] 
        IF header_size == 0: 
            Print ( 'file does not exist' ) 
        the else: 
            header_types = self.socket.recv (header_size) 
            header_dic = The pickle.loads (header_types) 
            Print (header_dic) 
            FILE_SIZE header_dic = [ 'FILE_SIZE']
            header_dic = filename [ 'filename'] 
 
            with Open ( '% S / S%'% (self.down_filepath, filename), 'WB') AS F: 
                recv_size = 0 
                the while recv_size <FILE_SIZE: 
                    RES = self.socket.recv ( self.max_recv_bytes) 
                    f.write (RES) 
                    recv_size + = len (RES) 
                    Print ( 'total size:% s downloaded:% s'% (FILE_SIZE, recv_size)) 
                the else: 
                    Print ( 'download successful!') 
 
    DEF PUT (Self, CMDS): 
        "" "Upload 
        1. Check the uploaded file exists 
        2. Upload file header_size 
        3. upload files header_bytes 
        4. open the file send the form to read (line)
        :param cmds: 上传的内容 eg: cmds = ['put','a.txt']
        :return:
        """
        filename = cmds[1]
        file_path = os.path.join(self.upload_filepath, filename)
        if os.path.isfile(file_path):
            file_size = os.path.getsize(file_path)
            header = {
                'filename': os.path.basename(filename),
                'md5': 'xxxxxx',
                'file_size': file_size
            }
            header_bytes = pickle.dumps(header)
            self.socket.send(struct.pack('i', len(header_bytes)))
            self.socket.send(header_bytes)
 
            with open(file_path, 'rb') as f:
                B = send_bytes '' 
                for in Line F: 
                    self.socket.send (Line) 
                    send_bytes + = Line 
                    Print ( 'Total size:% s uploaded:% s'% (FILE_SIZE, len (send_bytes))) 
                the else: 
                    Print ( 'uploaded successfully!') 
        the else: 
            Print ( 'file does not exist') 
 
 
ftp_client FTPClient = (( "127.0.0.1", 8080)) 
ftp_client.run () 
ftp_client.client_close ()

11, udp protocol-based programming, functional

  Server:

# _*_ coding: utf-8 _*_
import socket
ip_port = ('127.0.0.1',8808)
udp_server_client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
udp_server_client.bind(ip_port)
while True:
    conn,addr = udp_server_client.recvfrom(1024)
    print(conn,addr)
 
    udp_server_client.sendto(conn.upper(),addr)

  Client:

# _*_ coding: utf-8 _*_
import socket
ip_port = ('127.0.0.1',8808)
udp_server_client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
    cmd = input(">>>>").strip()
    if not cmd:
        continue
    udp_server_client.sendto(cmd.encode('utf-8'),ip_port)
    back_cmd,addr = udp_server_client.recvfrom(1024)
    print(back_cmd.decode('utf-8'))

UDP does not occur stick package, exemplified below

  Client:

# _*_ coding: utf-8 _*_
import socket
 
ip_port = ('127.0.0.1',8989)
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
client.sendto('hello'.encode('utf-8'),ip_port)
client.sendto('james'.encode('utf-8'),ip_port)
client.close()

  Server:

# _*_ coding: utf-8 _*_
import socket
 
ip_port = ('127.0.0.1',8989)
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind(ip_port)
res1 = server.recvfrom(5)
print("res1:",res1)
res2 = server.recvfrom(5)
print("res2:",res2)
server.close()

12, executes the specified command, so that the client can see the end of service time

Tip time.time, I do not think there is no sense

13, executes the specified command, so that the client can be synchronized with a time service

  Server:

# _*_ coding: utf-8 _*_
import socket
import subprocess
import time
 
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(('127.0.0.1', 8080))
while True:
    data, client_addr = server.recvfrom(1024)
    print(data, client_addr)
    obj = subprocess.Popen(data.decode('utf-8'),shell=True,  # time 命令在windows 下不能用
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    stdout = obj.stdout.read()
    stderr = obj.stderr.read()
    print(stdout+stderr)
    server.sendto(stdout+stderr,client_addr)
    if data.decode('utf-8') == 'time':
        str_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        # str_time = '2017-01-01 00:00:00'
        server.sendto(str_time.encode('gbk'), client_addr)
 
server.close()

  Client:

# _*_ coding: utf-8 _*_
import socket
import os
import time
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
while True:
    msg = input('>>>:').strip()
    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))
    data,server_addr = client.recvfrom(1024)
    print(data.decode('utf-8'),server_addr)
    localtime = time.localtime()
    os.system("date %d-%d-%d" % (localtime.tm_year, localtime.tm_mon, localtime.tm_mday))  # 设置日期
    os.system("time %d:%d:%d.0" % (localtime.tm_hour, localtime.tm_min, localtime.tm_sec))  # 设置时间
 
client.close()

Guess you like

Origin www.cnblogs.com/tu240302975/p/12449561.html