环境准备: 主机系统:Windows10
VMware版本:16.2.2
虚拟机版本:CentOS7.6
Python版本:3.9.8
一台用于服务端Centos7_UDPServer,一台用于客户端Centos7_UDPClient
# 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.
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()
操作前务必关闭防火墙,否则会出现疑难杂症!!!
[root@localhost PyCode]# systemctl stop firewalld
UDPServer.py
文件[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
[shun@localhost ~]$ ip addr
UDPServer.py
配置参数中的IP[root@localhost PyCode]# vim UDPServer.py
UDPServer.py
[root@localhost PyCode]# python3 -V
Python 3.9.8
[root@localhost PyCode]# python3 UDPServer.py
The server is ready to receive:
[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[root@localhost PyCode]# vim UDPClient.py
UDPClient.py
配置参数中的IP很多人都填错了,配置参数中的IP应该是UDPServer虚拟机的IP,而不是UDPClient虚拟机的IP!!!
UDPClient.py
[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虚拟机屏幕截图
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。