This blog is only used to record personal learning progress, and the knowledge is shallow. Welcome everyone to come and communicate. (Some materials sources network, if there is any infringement, delete it immediately)
All articles in my blog are purely studying and do not involve commercial interests. Not suitable for reference, delete yourself!
If it is used for illegal behavior, it has nothing to do with myself
Program based on the Python use TCP protocol to implement communication functions
import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create a socket object, the purpose of selecting AF_inet is to use IPv4 to communicate, and Sock_stream provides connected stable data transmission ip=socket.gethostname() #ip='' port=9999 s.connect((ip,port)) while True: while True: mes = input("Please enter the information you want to send:") if mes != "exit": T_mes = bytes(mes,encoding="gbk") s.send(T_mes) data=s.recv(1024) print(str(data,encoding="gbk")) else: break; if mes == "exit": break; s.close()
import socket import platform import psutil s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)# Create a socket object, the purpose of selecting AF_inet is to use IPv4 to communicate, and Sock_stream provides connected stable data transmission ip=socket.gethostname() #ip='' port=9999 s.bind((ip,port))# Binding port s.listen(5)# Set the maximum number of connections while True: print("The server starts, and is listening to the client connection") connet, addr = s.accept() # Create client connection while True: data=connet.recv(1024).decode() print('Client Send Content:',data) if data == "show me OS info": os = platform.uname() reply01 = bytes(str(os), encoding="gbk") connet.send(reply01) else: if data == "show me Disk info": m= a = str(psutil.disk_partitions())+str(len(psutil.disk_partitions())) reply02 = bytes(a,encoding="gbk") connet.send(reply02) else: reply00 = bytes("Command invalid \ n", encoding="gbk") connet.send(reply00) s.close()
- First start the server
- and then start the client
- You can send the specified command, but you need to write it yourself. I wrote a check OS version and a disk partition inquiries.
show me OS info
show me Disk info
- The server also has data from the client sent by the client