写在前面
环境准备: 主机系统:Windows10
VMware版本:16.2.2
虚拟机版本:CentOS7.6
Python版本:3.9.8
准备两台虚拟机
一台用于服务端Centos7_UDPServer,一台用于客户端Centos7_UDPClient
源代码文件
UDPServer.py
代码语言:python代码运行次数:0复制# a simple udp app
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('192.168.88.88', serverPort)) # ('192.168.88.88', serverPort) is a tuple
print('The server is ready to receive: ')
i = 1
while True:
message, clientAddress = serverSocket.recvfrom(2048)
while i > 0:
print(clientAddress)
print("The data type of clientAddress is :", type(clientAddress))
i -= 1
print(message)
# python对bytes类型的数据用带b前缀的单引号或双引号表示:
print(type(message)) # <class 'bytes'>
print(message.decode())
modifiedMessage = message.decode().upper()
# print(modifiedMessage)
serverSocket.sendto(modifiedMessage.encode(), clientAddress)
# The server must attach a destination address to the packet before dropping it into the socket.
UDPClient.py
代码语言:python代码运行次数:0复制from socket import *
# serverName = "hostname"
serverName = "192.168.30.130"
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
while True:
message = input("Input lowercase sentence:")
clientSocket.sendto(message.encode(), (serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
# With the above line, when a packet arrives from the Internet at the client’s socket, the packet’s data is
# put into the variable modifiedMessage and the packet’s source address is put into the variable serverAddress.
# The program UDPClient doesn't actually need this server address information,
# since it already knows the server address from the outset;
# but this line of Python provides the server address nevertheless.
# The method recvfrom also takes the buffer size 2048 as input.
print("Data from the UDP server:", modifiedMessage.decode())
clientSocket.close()
注意事项
操作前务必关闭防火墙,否则会出现疑难杂症!!!
启动Centos7_UDPServer虚拟机
CentOS 关闭防火墙
代码语言:shell复制[root@localhost PyCode]# systemctl stop firewalld
拷贝UDPServer.py
文件
代码语言:shell复制[shun@localhost ~]$ su root
密码:
[root@localhost shun]# cd /home
[root@localhost home]# mkdir PyCode
[root@localhost home]# cd /mnt/hgfs/Share/
[root@localhost Share]# ls
curve01.py TCPClient.py TCPServer.py UDPClient.py UDPServer.py
[root@localhost Share]# cp -f UDPServer.py /home/PyCode/
[root@localhost Share]# cd /home/PyCode/
[root@localhost PyCode]# ls
UDPServer.py
CentOS 查看本机IP
代码语言:shell复制[shun@localhost ~]$ ip addr
修改UDPServer.py
配置参数中的IP
代码语言:shell复制[root@localhost PyCode]# vim UDPServer.py
运行UDPServer.py
代码语言:shell复制[root@localhost PyCode]# python3 -V
Python 3.9.8
[root@localhost PyCode]# python3 UDPServer.py
The server is ready to receive:
启动Centos7_UDPClient虚拟机
代码语言:shell复制[shun@localhost ~]$ su root
密码:
[root@localhost shun]# cd /home
[root@localhost home]# mkdir PyCode
[root@localhost home]# cd PyCode/
[root@localhost PyCode]# cd /mnt/hgfs/Share/
[root@localhost Share]# ls
curve01.py TCPClient.py TCPServer.py UDPClient.py UDPServer.py
[root@localhost Share]# cp -f UDPClient.py /home/PyCode/
[root@localhost Share]# cd /home/PyCode/
[root@localhost PyCode]# ls
UDPClient.py
修改UDPClient.py
配置参数中的IP
代码语言:shell复制[root@localhost PyCode]# vim UDPClient.py
!!!注意事项!!!
UDPClient.py
配置参数中的IP很多人都填错了,配置参数中的IP应该是UDPServer虚拟机的IP,而不是UDPClient虚拟机的IP!!!
运行UDPClient.py
代码语言:shell复制[root@localhost PyCode]# python3 -V
Python 3.9.8
[root@localhost PyCode]# python3 UDPClient.py
Input lowercase sentence:i love china
Data from the UDP server: I LOVE CHINA
实践结果
UDPClient虚拟机
发送了一个小写的字符串给UDPServer虚拟机
,并且UDPClient虚拟机
接收到了UDPServer虚拟机
处理字符串小写变大写的结果
UDPClient虚拟机屏幕截图
UDPServer虚拟机屏幕截图