python - 有没有办法通过 UDP 发送大文件
问题描述
我刚刚开始使用 Python 学习网络编程。目前正在处理请求使用 UDP 将文件从客户端发送到服务器然后返回客户端的作业。但是,我的代码适用于小文件,但对于较大的文件有问题。
我找到了一种使用 import os 来找出文件大小的方法。所以我正在考虑分割文件,如果它大于UDP可以发送的最大大小,并一个接一个地发送分割文件。
但是我不确定 UDP 可以发送的最大大小是多少,以及在发送时如何将所有拆分部分重新构建在一起?
// Client side
import os
from socket import *
//Set up information of server
server_name = '127.0.0.1' # 127.0.0.1 is local IP
server_port = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)
//Request the location of file
foundFile = 0
while foundFile == 0:
print("========================================")
print("[Please input the path of the file you wish to transmit.]")
path = input("[Path]: ")
print("========================================")
if os.path.isfile(path):
print("[File found.]")
fileSize = os.path.getsize(path)
print("[File size is : ", fileSize, 'bits]')
print("[Starting file transmission.]")
foundFile = 1
else:
print("[File not found.] Please re-enter the correct path.")
print("[Please choose only on file, or enter the correct path.]")
foundFile = 0
fileOpen = open(path, 'rb')
file = fileOpen.read()
clientSocket.sendto(file, (server_name, server_port))
因此,如果文件很小,它应该发送到服务器端。稍后在代码中,服务器端将返回相同的内容并在桌面上创建一个副本。
但是如果文件太大会报错:clientSocket.sendto(file, (server_name, server_port)) OSError: [WinError 10040]
解决方案
推荐阅读
- arrays - How to create two dimensional Möbius strip, Klein bottle and projective plane arrays?
- gnuplot - Gnuplot - Multiplot autoscale displays x-axis and y-axis units twice on each other
- javascript - Slideout.js 菜单不流畅
- c# - How to keep a cell active (not just selected/highlighted) when you refresh a DataGridView?
- python - 如何使用顺序颜色函数中的颜色迭代地绘制线条
- angular6 - 如何将 Angular 6 表单提交到另一个页面 [不是 api] 并从 .
- reverse-engineering - 如何在离线安装中安装radare2插件?
- spring-boot - Fail to setup Keycloak with Spring Boot Security UnsatisfiedDependencyException with KeycloakAutoConfiguration
- vb.net - 如何为接受整数数据类型的公共属性赋值
- android - recyclerview 不能转换 UniversityViewHolder'?