OSI seven-layer model (open System interconnection, open systems interconnect)
Application -Layer network processes access the application layer: provides network services for application processes such as e-mail, file transfer, and terminal emulation, and provides user authentication presentation layer Data representation: ensures that the receiving system can read the data; Format data, construct data, negotiate data transfer syntax for application tiers, provide encrypted session-level inter-host communication: establish, manage, and terminate conversation transport-layer transfer issues between applications: Ensure the reliability of data transmission , establish, maintain and terminate the virtual circuit, Pass error detection and recovery, information flow control to ensure the reliability of network layer data transmission: route packet; Select the best path to pass the data; supports logical addressing and path selection Data link layer Access media: defines how data is formatted for transmission and how to control access to the network; support for error detection physical layer binary transmission: Unit bit
Socket instantiation of a socket
Bind binding to address and port
Listen started listening.
Accept waits for wait incoming data
Recv Accept Data
Send data (default send size is 32768 (32k) size)
Sendall Send all data
Close socket
A simple communication process
Service side:
Import socket# instantiation, binding, listening, waiting, deconstructing (identity, Ip-port), receiving, responding, shutting down server = Socket.socket () server.bind ((' localhost ', 6969)) Server.listen () print (' Start listening ... ') conn,addr = Server.accept () print (conn,addr) data = CONN.RECV (1024x768) print (' Server-side received: ', Data.decode ()) conn.send (' Hi, I am the server. ') Encode ()) Server.close ()
Client:
Import socket# instantiation, connection, send, receive response, close client = Socket.socket () client.connect ((' localhost ', 6969)) client.send (' Hi,i am the Client. '. Encode ()) print (' Send data ... ') data = CLIENT.RECV Print (' Client received data: ', Data.decode ()) Client.close ()
Output Result:
1. Run the server first, listen to the port, and start waiting for incoming data:
Start listening ...
2. Start the client, send the data, receive the response:
Send data ... The client receives the data: Hi, I am the server.
3. Go back to the server to view the received data:
Start listening ... <socket.socket fd=6, family=addressfamily.af_inet, Type=socketkind.sock_stream, proto=0, Laddr= (' 127.0.0.1 ', 6969), raddr= (' 127.0.0.1 ', 55149) > (' 127.0.0.1 ', 55149) received data: Hi,i am the client.
Second, analog SSH remote command execution
Server-side:
Import Socketimport timeimport os# instantiation, binding, listening, waiting, deconstructing (identity, Ip-port), receiving, responding, shutting down server = Socket.socket () server.bind (' localhost ', 6969)) Server.listen (3) #表示当正在处理一个连接时, you can suspend up to the next 3 connections in order (which can be understood as queues), and if a 4th is connected, a timeout exception will be thrown after a certain amount of time: Timeouterror : [Errno] Operation timed Outprint (' Start listening ... ') Try: while True: conn,addr = server.accept () # PRINT (conn, Addr) Peerip,peerport = Conn.getpeername () localip,localport = Conn.getsockname () print (' {}:{}--and {} : {} '. Format (peerip,peerport,localip,localport)) while True: data = CONN.RECV (1024x768) if not data: Break print (Data.decode ()) ret = Os.popen (Data.decode ()). Read () conn.send (Ret.encode ()) Server.close () except Keyboardinterrupt as E: print (' connection interrupted ... ')
Client
Import socket# instantiation, connection, send, receive response, close client = Socket.socket () client.connect ((' localhost ', 6969)) print (' Connecting server ... ') while True: msg = input (' >>> '). Strip () # print (' sending data ... ') if not msg: continue Client.send (Msg.encode ()) # print (' Sent ... ') data = Client.recv (1024x768) if not data: Break Print (Data.decode ()) Client.close ()
Operation Result:
1. Service side:
Start listening ...
2. The client (you can enter the executed command to receive the execution result returned by the server):
Connecting to service side ... >>>ls-ltotal 48-rw-r--r-- 1 Zhangsan staff 222 Nov 10:37 1.0.py-rw-r--r-- 1 Zhangsan Staff 433-16:49 2.0.py-rw-r--r-- 1 Zhangsan staff 158 Nov 10 23:18 error.log-rw-r--r-- 1 Zhangsan staff 437 Nov 14:10 socket_client1.py-rw-r--r-- 1 Zhangsan staff 1010-14:21 socket_server1.py-rw-r--r-- 1 Zhangsan staff 608 Nov 10 23:31 exception handling. py >>>ls-l. /total 0drwxr-xr-x 7 Zhangsan staff 224 Nov 8 12:51 1106drwxr-xr-x one zhangsan staff 352 9 11:33 1108drwxr-xr-x 8 Zhangsan Staff-- 14:21 1110>>>
3. Service side:
Start listening ... 127.0.0.1:56380--127.0.0.1:6969ls-lls-l. / #记录执行的命令
Protocol cluster:
Af_inet IPV4
Af_inet6 IPV6
Af_unix Local Address
Protocol: Protocol
Sock_stream TCP
Sock_dgram UDP
Sock_raw raw sockets, can forge data such as source IP
Python Simple socket emulation SSH