首页 > 解决方案 > 有没有办法通过 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]

标签: pythonudp

解决方案


推荐阅读