Table of contents
Foreword:
In the last issue, we initially learned about the concept of network communication (Link Network Programming 1.0-----Introduction to Network and Communication_Grey Le Tad's Blog-CSDN Blog ), then in this issue we will continue to learn how to communicate through network programming. Realize the information transfer between server and client.
Socket programming
1 Introduction
Socket programming is a protocol-independent network programming interface through which applications can send or receive data, which can be opened, read, written, and closed like files.
Socket is an intermediate software abstraction layer for communication between the application layer and the TCP/IP protocol family, and it is a set of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is all, and Socket organizes data to meet the specified protocol.
2. socket in Python
There is already a socket package module in Python. When we want to implement socket programming through Python, we only need to import this module, and then we can build a LAN server and client according to the method inside.
import module
import socket
Implementation of the server
Every time we send a message on WeChat or QQ, the message we send will pass through the local area network and then be transmitted to the public network, and then transmitted to the server bound to the public network, and then the server will pass it down through the network protocol receiver. Network communication is probably such a process, so here I use the tcp protocol to implement the code of the direct server of the LAN
import random
import socket
#1.创建服务器对象
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 参数1:socket.AF_INET, 表示 ipv4地址
# 参数2:socket.SOCK_STREAM表示 tcp协议封装好的参数
#2.获取当前计算机地址的代码
p=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
p.connect(('8.8.8.8',80))
nowip=p.getsockname()[0]
p.close()
port=8888
print('当前的ip地址:',nowip)
print('当前端口号:',port)
#3.把服务器绑定到本计算机上
server.bind((nowip,port))
#4.设置监听模式,设置最大的监听数
server.listen(128) #设置最大的连接数128
print('服务端已启动,等待连接……')
#5.等待客户的连接
conn, addr = server.accept()
# conn 是连接客户端的套接字对象
# addr 是表示客户端的连接地址
while 1:
try:
#6.接受客户端的数据
#网络数据都是以字节流的形式去发送的
data=conn.recv(1024).decode('utf8') #参数1024表示每次接受最大的字节数,decode进行解码,由字节流编码转换为字符
if not data:#当接受到空数据的时候就说明客户端断开了连接
print(f'{addr}断开了连接')
break
print(f'----{addr}发送了----',data)
#7.返回一个0~100的随机数数据给客户端
conn.send(str(random.randint(0,100)).encode('utf8'))#进行字节流编码发送
except ConnectionResetError:
print('error')
break
#7.关闭套接字对象
conn.close()
#8.关闭服务器
server.close()
Client implementation
The code implementation of the client is also similar to that of the server. To implement it, you must first create a client object, then connect to the address and port number of the server, and then you can send and receive information with the server. code show as below:
#客户端
import socket
#1.创建客户端对象
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#2.连接服务器地址和端口
client.connect(('10.33.110.204',8888))
count=1 #计数
while 1:
a=input(r'输入你要发送的内容(\q退出):').strip()
if a==r'\q':
break
if not a: #当发送的数据为空时就重新输入
print('发送的数据不能为空')
continue
#3.把数据发送给服务器
client.send(a.encode('utf8'))
#4.获取到服务器发送的数据
data = client.recv(1024) # receive
if not data:
break
print(f'对方发送的消息--{count}--:',data.decode("utf8"))
count+=1
#5.关闭客户端
client.close()
(Note: The above code function is only in the local area network to realize the sending and receiving of information between the server and the client. If the two devices are connected to different WiFi, then the information cannot be sent)
Well, the above is the whole content of this issue, see you in the next issue!
Share a wallpaper:
(Violet Evergarden)