Python socket模块ftp传输文件过程解析
作者:星辉笑 时间:2021-04-17 02:22:59
这篇文章主要介绍了Python socket模块ftp传输文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用环境:python3,window环境,需要在头部声明# -*- coding:utf-8 -*-
实现功能:
将sever端所处文件夹的文件,传输到client端所处的文件夹中。
并且通过md5检测是否出错。
客户端命令的形式是: get 文件名
client处的新文件是 文件名.new
ftp_sever.py
import hashlib
import socket ,os,time
server = socket.socket()
server.bind(('localhost',9999))
server.listen()
while True:
print("I am waiting for connection.")
conn, addr = server.accept()
print("new conn:",addr)
while True:
print("等待新指令")
data = conn.recv(1024)
if not data:
print("客户端已断开")
break
cmd,filename = data.decode().split()
print(filename)
if os.path.isfile(filename): #判断是否该文件名为文件
f = open(filename,"rb")
m = hashlib.md5() #为md5准备
file_size = os.stat(filename).st_size #利用os.stat获取文件的大小
conn.send( str(file_size).encode() ) #send file size
conn.recv(1024) #等待确认,同时可以防止粘包。
for line in f: #一行一行发送数据,同时更新md5
m.update(line) #不断更新md5
conn.send(line) #不断发送数据。
print("file md5", m.hexdigest()) #十六进制的md5
f.close()
conn.send(m.hexdigest().encode()) #send md5
print("send done")
server.close()
ftp_client.py
import socket
import hashlib
client = socket.socket()
client.connect(('localhost', 9999))
while True:
cmd = input(">>:").strip() #形式 get filename
if len(cmd) == 0: continue
if cmd.startswith("get"):
client.send(cmd.encode()) #发送命令,形式 get filename
server_response = client.recv(1024) #接收文件大小信息
print("servr response:", server_response)
client.send(b"ready to recv file") #发送确认信息。
file_total_size = int(server_response.decode()) #将文件大小int化。
received_size = 0 #初始化接收数据大小,为0
filename = cmd.split()[1] #获取文件名
f = open(filename + ".new", "wb") #以二进制形式写入
m = hashlib.md5() #为md5准备
while received_size != file_total_size:
'''下面的if判断是用来完整接收文件,从而避免粘包。'''
if file_total_size - received_size > 1024: # 要收不止一次 24 size = 1024 25 else: # 最后一次了,剩多少收多少 26 size = file_total_size - received_size 27 print("last receive:", size) 28 data = client.recv(size) #data只需要是一小个内存,大小为1k就好 29 received_size += len(data) 30 m.update(data) #不断更新md5 31 f.write(data) #不断写入 32 # print(file_total_size,received_size) 33 else: 34 new_file_md5 = m.hexdigest() #获取十六进制的md5 35 print("file recv done", received_size, file_total_size) 36 f.close() 37 server_file_md5 = client.recv(1024) #接收md5值 38 print("server file md5:", server_file_md5) 39 print("client file md5:", new_file_md5) 40 41 client.close()
来源:https://www.cnblogs.com/Ian-learning/p/8460211.html
标签:python,socket,模块,ftp,传输,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
十分钟轻松掌握dataframe数据选择
2021-03-03 11:11:40
Python实现邮件的批量发送的示例代码
2023-08-09 07:47:57
mysql数据库你需要特别注意的23个事项
2010-08-08 08:34:00
Python selenium文件上传方法汇总
2022-12-01 04:47:29
Python LMDB库的使用示例
2021-01-31 15:11:25
PHP文件上传功能实现逻辑分析
2023-05-25 02:28:30
Python实现图片滑动式验证识别方法
2023-11-05 22:14:52
![](https://img.aspxhome.com/file/2023/3/94183_0s.png)
asp 存储过程分页代码第1/2页
2011-04-03 10:39:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112228776s.jpg)
纯数字不重复排列的另类方法
2009-12-04 18:25:00
python matplotlib实现将图例放在图外
2021-11-19 06:55:45
![](https://img.aspxhome.com/file/2023/2/85122_0s.jpg)
RSA加密解密算法 asp源码
2009-08-28 13:10:00
Python:format格式化字符串详解
2021-02-11 19:23:58
别人复制你网站的文章时自动加上注释
2009-02-09 13:20:00
条件CSS的使用[译]
2009-03-12 12:23:00
![](https://img.aspxhome.com/file/UploadPic/20093/12/01-51s.jpg)
PHP实现页面静态化的超简单方法
2023-11-18 17:13:24
![](https://img.aspxhome.com/file/2023/2/72682_0s.jpg)
Python编程使用DRF实现一次性验证码OTP
2021-07-30 00:25:26
python实现余弦相似度文本比较的示例
2023-01-18 09:17:48
![](https://img.aspxhome.com/file/2023/2/81462_0s.png)
解决用CSS控制DIV居中失效的问题
2010-04-05 21:53:00
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
2023-07-29 00:03:51
分析描述CentOS mysql安装系统
2010-10-25 20:34:00