前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VMware虚拟机基于Python3实现简单的通信

VMware虚拟机基于Python3实现简单的通信

原创
作者头像
编程小顺
修改2022-05-05 11:56:53
3820
修改2022-05-05 11:56:53
举报
文章被收录于专栏:知识小木屋知识小木屋

写在前面

环境准备: 主机系统:Windows10

VMware版本:16.2.2

虚拟机版本:CentOS7.6

Python版本:3.9.8

准备两台虚拟机

一台用于服务端Centos7_UDPServer,一台用于客户端Centos7_UDPClient

准备两台虚拟机
准备两台虚拟机

源代码文件

UDPServer.py
代码语言:python
复制
# 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
复制
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()

注意事项

操作前务必关闭防火墙,否则会出现疑难杂症!!!

关闭主机防火墙.png
关闭主机防火墙.png

启动Centos7_UDPServer虚拟机

CentOS 关闭防火墙

代码语言:shell
复制
[root@localhost PyCode]# systemctl stop firewalld
CentOS 关闭防火墙.png
CentOS 关闭防火墙.png

拷贝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
CentOS 查看本机IP.png
CentOS 查看本机IP.png

修改UDPServer.py配置参数中的IP

代码语言:shell
复制
[root@localhost PyCode]# vim UDPServer.py 
修改`UDPServer.py`配置参数中的IP.png
修改`UDPServer.py`配置参数中的IP.png

运行UDPServer.py

代码语言:shell
复制
[root@localhost PyCode]# python3 -V
Python 3.9.8
[root@localhost PyCode]# python3 UDPServer.py 
The server is ready to receive: 
运行UDPServer.py.png
运行UDPServer.py.png

启动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`配置参数中的IP
修改`UDPClient.py`配置参数中的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虚拟机屏幕截图

UDPClient虚拟机屏幕截图.png
UDPClient虚拟机屏幕截图.png

UDPServer虚拟机屏幕截图

UDPServer虚拟机屏幕截图.png
UDPServer虚拟机屏幕截图.png

微信公众号:编程小顺

微信公众号:编程小顺
微信公众号:编程小顺

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在前面
    • 准备两台虚拟机
      • 源代码文件
        • UDPServer.py
        • UDPClient.py
      • 注意事项
      • 启动Centos7_UDPServer虚拟机
        • CentOS 关闭防火墙
          • 拷贝UDPServer.py文件
            • CentOS 查看本机IP
              • 修改UDPServer.py配置参数中的IP
                • 运行UDPServer.py
                • 启动Centos7_UDPClient虚拟机
                  • 修改UDPClient.py配置参数中的IP
                    • !!!注意事项!!!
                  • 运行UDPClient.py
                  • 实践结果
                  • 微信公众号:编程小顺
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档