What is network programming ?
In the network environment , How to achieve Not in the same physical location Data communication between computers in
If you want to ensure the smooth completion of data communication , You need to understand the following concepts first :
Data communication between processes in different computers , The data needs to be analyzed first Package or pack Before transmission . The so-called protocol refers to the data packaging format that both communication parties need to abide by .
Just like mailing goods in the real world :
Protocol is the first operation element to ensure that data can be transmitted correctly , The protocol specification followed on the Internet is called TCP/IP agreement .
In the network environment , You need to specify an address for each computer , This address is IP Address , Actually IP Address is a logical address , And each computer has a physical address , It's on the network card MAC Address .
IP Address and MAC The difference in address ?
Multiple network software can be installed on one computer , Such as QQ、 browser 、 Network game ……
How does the operating system distinguish which software should handle multiple data from the network at the same time ?
The port is equivalent to one assigned by the operating system to each network software House plate identification symbol , It is used to correctly divert multiple data streams input from the network to the corresponding process .
When one computer sends data to another computer :
It's kind of like visiting friends :
First encapsulate a gift box
Then go to visit according to the address and house number told by your friend
TCP Is a transport layer protocol , It is a reliable connection oriented transport layer protocol .
Define a function for specific data interaction , Called by a child thread .
import socket # socket modular
import time # Time module
import threading # Thread module
def session(sock, addr):
print(' Welcome new %s:%s...' % addr)
sock.send(b'Welcome!')
while True:
data = sock.recv(1024)
time.sleep(1)
# Decode data
if not data or data.decode('utf-8') == 'exit':
break
sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
sock.close()
print(' From %s:%s The connection is closed .' % addr)
establish socket listening object :
# establish TCP socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Listening port
server.bind(('127.0.0.1',1234))
server.listen(5)
print(' The server is waiting for the client to connect ……')
Establish a service listening socket , You need to specify the Service type :
while True:
# Accept a new connection :
sock, addr = s.accept()
# Create a new thread to handle TCP Connect :
t = threading.Thread(target=session, args=(sock, addr)) t.start()
When a customer is connected , Start the thread to complete the specific data processing .
The client code is relatively simple .
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Establishing a connection :
client.connect(('127.0.0.1', 1234))
# Receive a welcome message :
print(client.recv(1024).decode('utf-8'))
for data in [b'Rose', b'Think', b'Babala']:
# send data :
s.send(data)
print(s.recv(1024).decode('utf-8'))
s.send(b'exit')
s.close()
test result :
Server side
client :
Python Relevant modules are provided , Encapsulates the underlying concrete code logic , For developers , Just follow the process step by step , If you need to better understand the whole process of network communication , You need to know relevant network knowledge .