This is a network programming a small step, a big step but it is my

1. Arrange the TCP three-way handshake, four waved map

The first is the three-way handshake:

1 First, the client sends a request to a server

Server 2 in the receiving state, and the received request, the flow returns an acknowledgment to the client, but also sends a request to the server

3 client receives a confirmation and request, so the two began formal diplomatic relations

Four wave:

1. The client made a request to cut off the connection to the server

2. The server receives the request and confirm whether there are tasks not performed, no matter how many times will perform to the client

3. The server waiting to enter the closed state

4. The client also enters time_wait state, then the memory it can dissipate

2. After developing a remote TCP CMD program client connection with the server, the server can send commands to the server based on the execution command is received, whether or not executed successfully, regardless of execution several times, the results will be returned to the client Note: Execution System command using subprocess module is completed.

Server

import subprocess
import re
import socket
server=socket.socket()

server.bind(
    ('127.0.0.1',9527)#用于寻找服务端
)
server.listen(0)#只有一个人能上我


conn,addr=server.accept()
print(addr)

while True:
    try:
        data=conn.recv(1024).decode('utf8')
        print(data)
        # back_list=[]
        back=subprocess.check_output(data,shell=True)
        buck=back.strip()
        # back_list.append(back.split)
        # back=back.strip()
        print(buck)


    except Exception as e:
        print(e)
        break
conn.close()

Client

import subprocess
import socket
client=socket.socket()

client.connect(
    ('127.0.0.1',9527)#用于寻找服务端
)
while True:
    send_msg=input('输入命令给服务器:')
    client.send(send_msg.encode('utf8'))
    if send_msg=='q':
        break
    data=client.recv(1024).decode('utf8')
    print(data)
client.close()

Guess you like

Origin www.cnblogs.com/jimGraymane/p/11694421.html