1. Arrange the TCP three-way handshake, four waved map
In the three-way handshake know four times before waving
First of all you need to know what is a two-way channel, the channel is required to access the information transmission, bi-directional channel that two such paths, each responsible for different things, one is the client sends information to the server, and the other one is the server law information to the client.
Secondly, each time either the client or the server to send information, the other end must acknowledge receipt of the return information
It refers to a three-way handshake to establish a connection
Establishing the connection is to establish a two-way channel.
The first handshake: a client sends a connection request to the server, thus establishing a unidirectional channel to the client a server-side
The second handshake: server simultaneously sends an acknowledgment of receipt of the information and consent connections, the second channel is established
Third handshake: the client to the server to confirm receipt of information
2. TCP Remote CMD develop a program based on
the client server connection, you can send commands to the server
after the server receives the command is executed, regardless of whether the implementation is successful, regardless of execution several times, the results will be returned to the client
Note: Execution System command using subprocess module is completed.
Server
import socket
import subprocess
server = socket.socket()
server.bind(
('127.0.0.1',8000)
)
server.listen(5)
Listen for news
while True:
conn, addr = server.accept()
print(addr)
while True:
try:
data = conn.recv(1024).decode('gbk')
if data == 'q':
break
res = subprocess.check_output(data)
conn.send(res)
except Exception as e:
print(type(e))
conn.send(str(e).encode('gbk'))
print(e)
continue
conn.close()
Client
import socket
client = socket.socket()
client.connect(
('127.0.0.1',8000)
)
True the while:
send_msg = INPUT ( 'enter the command:')
client.send (send_msg.encode ( 'GBK'))
if send_msg == 'q':
break
data = client.recv(1024).decode('gbk')
print(data)
client.close()
'' '
Client operating results
Please enter the command: dsaf
[WinError 2] system can not find the file specified.
Please enter the command: ipconfig
Windows IP Configuration
Wireless LAN Adapter WLAN:
Media state:............ Media disconnected
connection-specific DNS suffixes.......:
Wireless LAN adapter Local Area Connection * 9:
Media state:............ Media disconnected
connection-specific DNS suffixes.......:
Wireless LAN adapter Local Area Connection * 10:
Media state:............ Media disconnected
connection-specific DNS suffixes:.......
'' '