python通过UDP传输⽂件使⽤⼀个简单的python脚本将⼀个本地⽂件以码流的形式,通过UDP协议发送到对端:
import socket
import os
import stat
杭州西湖限行import struct
MAX_PACK_SIZE = 100
DEST_IP = 'localhost'
DEST_PORT = 17800
filename = raw_input("input filename")
filesize = os.stat(filename)[stat.ST_SIZE]
f = open(filename, "rb")
chList = []
for i in range(0, filesize):
(ch,) = struct.unpack("B", f.read(1))
chList.append(ch)
client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
packSize = 0
string = ""
args = []启用dhcp>中药补骨脂
学习句子for i in range(0, filesize):
packSize = packSize + 1
string = string + struct.pack("B", chList[i])
if (MAX_PACK_SIZE == packSize or i == filesize - 1):
client.ndto(string, (DEST_IP, DEST_PORT))如何屏蔽短信
湖南省的大学
packSize = 0
string = ""
client.clo()
使⽤到的知识点:
1、UDP客户端最简单的实现:婚戒戴哪只手女方
client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
client.ndto(string, (DEST_IP, DEST_PORT))
梦见领导人
client.clo()
2、python中对码流的操作:struct.pack 和 struct.unpack的使⽤
(ch,) = struct.unpack("B", f.read(1))
string = string + struct.pack("B", chList[i])
3、⼆进制⽂件的读取:
f = open(filename, "rb")
4、获得系统中某个⽂件的字节数
filesize = os.stat(filename)[stat.ST_SIZE]