python简单的ftp程序
服务器端
'''
1.读取文件名
2.检测文件是否存在
3.打开文件
4.检测文件大小
5.发送文件大小给客户端
6.等客户端确认
7.开始边读边发数据
8.发送md5
'''
import socket,os,time,hashlib
server=socket.socket()
server.bind(('0.0.0.0',9999))
server.listen() while True:
conn, addr = server.accept()
print("new conn:",addr)
# print("recv:%s" % data)
while True:
print("等待新指令:")
data = conn.recv(10240)#[1]收到的是比特流
if not data:
print("客户端已断开")
break
cmd, filename = data.decode().split()#比特流解码之后按照空格分割成"命令,文件名"
print("文件名:", filename)
if os.path.isfile(filename):#判断文件名是否存在
f = open (filename, 'rb')
m = hashlib.md5()
file_size = os.stat(filename).st_size
#print(type(file_size)) #int
conn.send (str (file_size).encode ()) # [2]发送文件大小
print("[服务器发送的文件大小]",str(file_size).encode())
conn.send(str(file_size).encode())#[2]发送文件大小
#conn.send(str(len(file_size.encode())).encode("utf-8")) # 发送文件大小
#conn.send(file_size.encode())
conn.recv(1024)#等待ack[3]
# for line in f:
for line in f:
m.update(line)
#line = f.readline(1024)
conn.send(line) # [4]服务器端一行一行发送
print("发送的数据:",line.decode())
print("文件md5值",m.hexdigest())
#conn.send(m.hexdigest().encode())#[4]发送md5
f.close()
print("send done") server.close() 客户端
import socket,os,sys,hashlib client = socket.socket()
client.connect(('localhost', 9999)) while True:
cmd = input("input the filename>>>").strip()
if len(cmd)== 0: continue
if cmd.startswith("get"):#以'get'开头才行
client.send(cmd.encode())#[1]输入的是字符串.实际发送的是比特流
server_response=client.recv(1024)#[2]客户端收到来自服务器发送的文件的大小
print("[server_response]文件大小:",server_response)
client.send(b"ready to accept......")#[3]
file_total_size=int(server_response.decode())#因为收到来自服务器的是个比特流,所以要先解码,完了之后将其变成整数,方便下面的比较
print("[file_total_size]:",file_total_size)
received_size=0
# received_data=b''
filename=cmd.split()[1]
f=open(filename+".new","wb")
m=hashlib.md5()
while received_size != file_total_size:
data=client.recv(1024)#[4]客户端收到数据
received_size += len (data) # 每次收到的有可能小于1024,所以必须用len判断
print ("文件总大小:%s,已收到文件大小:%s" % (file_total_size, received_size))
f.write(data)
print("data类型:",type(data))
print("data.decode类型:",type(data.decode()))#收到的数据解码之后是字符类型
print("当前文件大小",len(data.decode()))
#received_data+=data
m.update(data)
#print(data.decode())
else:
new_file_md5=m.hexdigest()
print("cmd res raceive done...",received_size)
f.close()
# server_file_md5=client.recv(1024)
# print("server file md5:",server_file_md5)
# print("client file md5",new_file_md5) client.close()
python简单的ftp程序的更多相关文章
- Python简单基础小程序
1 九九乘法表 for i in range(9):#从0循环到8 i += 1#等价于 i = i+1 for j in range(i):#从0循环到i j += 1 print(j,'*',i, ...
- 用python开发简单ftp程序
根据alex老师视频开发的简单ftp程序,只能实现简单的get功能 ftp客户端程序: #!/usr/bin/env python #_*_ coding:utf-8 _*_ import socke ...
- python实现FTP程序
python实现FTP程序 程序源码 上传功能 查看文件 cd功能 创建目录 程序源码 目录结构 服务端 主程序 import optparse import socketserver import ...
- Python开发程序:FTP程序
作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp se ...
- python简单的监控脚本-利用socket、psutil阻止远程主机运行特定程序
python简单的监控脚本-利用socket.psutil阻止远程主机运行特定程序 psutil是一个跨平台的库(http://code.google.com/p/psutil/),能够轻松的实现获取 ...
- 用python写个简单的小程序,编译成exe跑在win10上
每天的工作其实很无聊,早知道应该去IT公司闯荡的.最近的工作内容是每逢一个整点,从早7点到晚11点,去查一次客流数据,整理到表格中,上交给素未蒙面的上线,由他呈交领导查阅. 人的精力毕竟是有限的,所以 ...
- (转)Python开发程序:支持多用户在线的FTP程序
原文链接:http://www.itnose.net/detail/6642756.html 作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ...
- [ python ] 项目一:FTP程序
声明: 该项目参考学习地址: http://www.cnblogs.com/lianzhilei/p/5869205.html , 感谢博主分享,如有侵权,立即删除. 作业:开发一个支持多用户在线的F ...
- Python开发【项目】:FTP程序
作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp se ...
随机推荐
- 泡泡一分钟:Motion Planning for a Small Aerobatic Fixed-Wing Unmanned Aerial Vehicle
Motion Planning for a Small Aerobatic Fixed-Wing Unmanned Aerial Vehicle Joshua Levin, Aditya Paranj ...
- 腾讯云服务器部署FTP
腾讯云服务器,在申请成功后就可以使用远程桌面了. FTP服务器端软件 下载地址:https://filezilla-project.org/download.php?type=server FTP客户 ...
- OCR技术(光学字符识别)
什么是OCR? OCR英文全称是optical character recognition,中文叫光学字符识别.它是利用光学技术和计算机技术把印在或者写在纸上的 文字读取出来,并转换成一种计算机能够接 ...
- LeetCode 669 Trim a Binary Search Tree 解题报告
题目要求 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so t ...
- linux strtock()函数使用问题
strtok()原型:char * strtok(char *s, const char *delim); 函数说明:strtok()用来将字符串分割成一个个片段.参数s 指向欲分割的字符串,参数de ...
- 【PyQt5-Qt Designer】在GUI中使用pyqtgraph绘图库
pyqtgraph绘图库 1.1 简介: pyqtgraph是Python平台上一种功能强大的2D/3D绘图库,相对于matplotlib库,由于内部实现方式上,使用了高速计算的numpy信号处理库以 ...
- kali蓝牙连接
http://blog.csdn.net/hailangnet/article/details/47723181 http://www.aiuxian.com/article/p-3012084.ht ...
- C++ 调用 opencv 读取视频文件列表并处理
//g++ trans_video.cpp -o trans_video `pkg-config opencv --libs --cflags` -L/usr/lib/x86_64-linux-gnu ...
- MongoDB limit 选取 skip跳过 sort排序 方法
MongoDB limit 选取 skip跳过 sort排序 在mysql里有order by MongoDB用sort代替order by > db.user.find() { " ...
- MongoDB pymongo模块 更新数据
现在chat集合里有3条数据 import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=27017, ...