码迷,mamicode.com
首页 > 编程语言 > 详细

python基础学习日志day8-socket上传文件

时间:2017-06-17 20:43:49      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:imp   split()   logs   data   send   int   class   命令   基础学习   

ftp server

1) 读取文件名

2)检查文件是否存在

3)打开文件

4)检查文件大小

5)发送文件大小给客户端

6)等客户端确认

7)开始边读边发数据

 

下载文件
客户端
# -*- coding:utf-8 -*- __author__ = shisanjun import socket import json import os host=("127.0.0.1",9000) client=socket.socket() client.connect(host) while True: cmd_str=input("请输入命令>>").strip() if len(cmd_str)==0:continue cmd_list=cmd_str.split() if cmd_list[0]=="put": if len(cmd_list)==1: print("not find put filename") else: filename=cmd_list[1] if os.path.isfile(filename): file_obj=open(filename,rb) filename2=filename.split("/")[-1] print(filename2) data_stat="%s %s" %(filename2,os.path.getsize(filename)) data={ "filename":filename2, "filesize":os.path.getsize(filename) } client.send(json.dumps(data).encode("utf-8")) for line in file_obj: client.send(line) file_obj.close() else: print("can not find file") elif cmd_list[0]=="get": pass 服务器端 # -*- coding:utf-8 -*- __author__ = shisanjun import socket import json host=("127.0.0.1",9000) server=socket.socket() server.bind(host) server.listen(5) while True: conn,addr=server.accept() data=conn.recv(4096) print(data.decode("utf-8")) data_dict=json.loads(data.decode("utf-8")) filename=data_dict.get("filename") f=open(filename,"wb") recesize=0 while recesize<data_dict.get("filesize"): recefile=conn.recv(4096) f.write(recefile) recesize+=len(recefile) f.close()

 

python基础学习日志day8-socket上传文件

标签:imp   split()   logs   data   send   int   class   命令   基础学习   

原文地址:http://www.cnblogs.com/lixiang1013/p/7041149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!