python 用udp传递文件,在python中发送/接收文件UDP

I've made this sending / receiving scripts but i corrupted file !

i have no idea why I'm getting this issue !

sender.py

#!/usr/bin/env python

from socket import *

import sys

s = socket(AF_INET,SOCK_DGRAM)

host =sys.argv[1]

port = 9999

buf =1024

addr = (host,port)

file_name=sys.argv[2]

f=open(file_name,"rb")

data = f.read(buf)

s.sendto(file_name,addr)

s.sendto(data,addr)

while (data):

if(s.sendto(data,addr)):

print "sending ..."

data = f.read(buf)

s.close()

f.close()

receiver.py

#!/usr/bin/env python

from socket import *

import sys

import select

host="0.0.0.0"

port = 9999

s = socket(AF_INET,SOCK_DGRAM)

s.bind((host,port))

addr = (host,port)

buf=1024

data,addr = s.recvfrom(buf)

print "Received File:",data.strip()

f = open(data.strip(),'wb')

data,addr = s.recvfrom(buf)

try:

while(data):

f.write(data)

s.settimeout(2)

data,addr = s.recvfrom(buf)

except timeout:

f.close()

s.close()

print "File Downloaded"

and this the original receiver that I've modify it (works fine 100%)

#!/usr/bin/env python

from socket import *

import sys

import select

host="0.0.0.0"

port = 9999

s = socket(AF_INET,SOCK_DGRAM)

s.bind((host,port))

addr = (host,port)

buf=1024

f = open("file.pdf",'wb')

data,addr = s.recvfrom(buf)

try:

while(data):

f.write(data)

s.settimeout(2)

data,addr = s.recvfrom(buf)

except timeout:

f.close()

s.close()

print "File Donwloaded"

as you notice it's making file at the beginning.

exacted:

client => send file (name.ext) => server:save it (name.ext)

my output :

corrupted file for pdf and empty for txt

解决方案

There are two problems here:

Syntax errors:

You're using a from socket import *. It's not an error on its own, but it becomes one when you do except socket.timeout.

Using UDP:

Using UDP, corruption shouldn't be a surprise. You probably don't want to be using UDP here, you should switch to TCP.

Here's why UDP is not appropriate here:

Packets may be lost but others could still reach their destination.

Packets may be duplicated

Packets may arrive in the wrong order

Note that switching to TCP will involve some refactoring of your code (it's a bit more complicated that just replacing SOCK_DGRAM with SOCK_STREAM), but in your case, you have to do it.

I'm not saying UDP is bad, but it's not appropriate in your case.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值