python Socket Programming Based on TCP Protocol

  • 2021-07-06 11:16:02
  • OfStack

Socket Programming Based on TCP Protocol

Realize telephone communication as an example, here is the transfer of characters, you can try to send 1 file


#  Server side 
import socket
# 1.  Accord with TCP Protocol mobile phone 
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # TCP
# 2.  Binding mobile phone number  1 Servers , If we use our own computer as a server, , Use your own IP Address 
server.bind(('127.0.0.1',8000)) # 127.0.0.1  On behalf of the local 
# server.bind(('192.168.11.251',8000)) 
server.listen(5) #  Semi-connection pool   Can be received at the same time 5 Clients 
# 3.  Waiting for client connection 
print('start...')
#  Link loop 
while True:
#  Communication loop 
conn,client_addr = server.accept()
while True:
try:
# 4.  Receive information  receive
data = conn.recv(1024) #  How many bytes are received at a time , If changed to 10, Client input dir,( Can be found in cmd Input in  dir, See what you get ) , What will happen , The next blog will talk about the problem of sticky packages , And how to deal with the problem of sticky package 
print(data)
# 5.  Back message 
conn.send(data.upper())
except ConnectionAbortedError:
continue
except ConnectionResetError:
break

#  Client 1
import socket
# 1.  Create conformance TCP Protocol mobile phone 
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.  Dial 
client.connect(('127.0.0.1',8000))
while True:
msg = input('please enter your msg') # dir
# 3.  Send a message 
client.send(msg.encode('utf8'))
# 4.  Receive a message 
data = client.recv(1024)
print(data)

#  Client 2
import socket
# 1.  Create conformance TCP Protocol mobile phone 
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.  Dial 
client.connect(('127.0.0.1',8000))
# msg = input('please enter your msg>>>')
# 3.  Send a message 
client.send('hello'.encode('utf8'))
client.send('world'.encode('utf8'))
# 4.  Receive information 
data = client.recv(1024)
print(data)

Simulate ssh remote execution command (linux system)


# ssh Server side 
import socket
import subprocess
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('192.168.11.251',8000)) #  This is my machine , The client can be someone else's machine 
server.listen(5)
print('start...')
while True:
conn,cient_addr = server.accept()
print(client_addr) #  It's printed from someone else's machine IP
while True:
try:
cmd = conn.recv(1024) # dir
print(cmd)
#  Help you execute cmd Command , Then save the execution results in the pipeline 
pipeline = subprocess.Popen(cmd.decode('utf8'),
shell = True,
stderr = subprocess.PIPE, # std  Standard 
stdout = subprocess.PIPE)
stderr = pipeline.stderr.read()
stdout = pipeline.stdout.read()
conn.send(stderr)
conn.send(stdout)
except ConnectionResetError:
break

# ssh Client 
import socket
# 1. Create conformance TCP Protocol mobile phone 
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2.  Dial 
client.connect(('192.168.11.210',8000))
while True:
msg = input('please enter your msg') # dir
# 3.  Send a message 
client.send(msg.encode('utf8'))
# 4.  Receive a message 
data = client.recv(10)
print(data.decode('gbk')) #  In mac It may be on the computer 'utf8'

The most effective way to learn is to have input and output, so that what you learn can really be useful


Related articles: