Python--day32--ftp文件传输报错的原因

解决办法:把buffer改小
server.py
#实现一个大文件的上传或下载
#配置文件 ip地址 端口号
import json
import socket
import struct
sk = socket.socket()
#起服务
sk.bind(('127.0.0.1', 8090))
sk.listen()
10 buffer = 1024
conn,addr = sk.accept()
#接收
head_len = conn.recv(4)
head_len = struct.unpack('i', head_len)[0]
json_head = conn.recv(head_len).decode('utf-8')
head = json.loads(json_head)
filesize = head['filesize']
with open(head['filename'], 'wb') as f:
while filesize:
print(filesize)
if filesize >= buffer:
content = conn.recv(buffer)
f.write(content)
filesize -= buffer
else:
content = conn.recv(filesize)
f.write(content)
break
conn.close()
sk.close()
client.py
#发送端
import os
import json
import struct
import socket
sk = socket.socket()
sk.connect(('127.0.0.1', 8090))
8 buffer = 1024 #发送文件
head = {'filepath':r'C:\Users\Administrator\Desktop',
'filename':r'test.txt',
'filesize':None}
fie_path = os.path.join(head['filepath'],head['filename'])
filesize = os.path.getsize(os.path.join(head['filepath'],head['filename'])) head['filesize'] = filesize
json_head = json.dumps(head) #字典转成了字符串
bytes_head = json_head.encode('utf-8') #字符串转bytes
print(json_head)
print(bytes_head)
#计算head的长度
head_len = len(bytes_head) #报头的长度
pack_len = struct.pack('i', head_len)
sk.send(pack_len) #先发报头的长度
sk.send(bytes_head) #再发送bytes类型的报头
with open(fie_path, 'rb') as f:
while filesize:
print(filesize)
if filesize >= buffer:
content = f.read(buffer) #每次读出来的内容
sk.send(content)
filesize -= buffer
else:
content = f.read(filesize)
sk.send(content)
break
sk.close()
Python--day32--ftp文件传输报错的原因的更多相关文章
- Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal multibyte sequence
Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal mul ...
- python引用py文件中文报错
文件 a.py 中引用文件 b.py 如果文件b.py中包含中文,会报错. 文件hello.py中代码如下: def say_nihao(): print "你好" 文件main. ...
- Python实现终端FTP文件传输
实现终端FTP文件传输 代码结构: .├── client.py├── readme.txt└── server.py 运行截图: readme.txt tftp文件服务器 项目功能: * 客户端有简 ...
- Python 基于Python实现Ftp文件上传,下载
基于Python实现Ftp文件上传,下载 by:授客 QQ:1033553122 测试环境: Ftp客户端:Windows平台 Ftp服务器:Linux平台 Python版本:Python 2.7 ...
- Python numpy 安装以及处理报错 is not a supported wheel on this platform
1. 安装 1)去这里搜索https://pypi.org/ 2)搜索框输入numpy 3)一般第一个就是搜索到的 4)点进去 5) Download files 点进去,找自己的版本 6)nu ...
- python安装包的时候报错
python安装包的时候报错 今天兴致勃勃的安装了一个paramiko包,过程很顺利,但是到结尾的时候报错,这就让人不爽了. 所以呢,需要安装一个名为python-dev的软件包. 该软件包包括头文件 ...
- FTP 文件传输服务
昨晚心血来潮,尝试用python写了一个ftp文件传输服务,可以接收指令,从远程ftp服务器同步指定目录数据,最后没用上,开源出来. https://github.com/jadepeng/ftp_t ...
- 【svn】在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted
1.svn在提交文件是报错:previous operation has not finished;run 'cleanup' if it was interrupted2.原因,工作队列被占用,只需 ...
- R.java 文件内报错:Underscores can only be used with source level 1.7 or greater。
R.java 文件内报错:Underscores can only be used with source level 1.7 or greater 网上查找后得知是Android工程图片资源命名的问 ...
随机推荐
- pytest相关资源收集
pytest官网 https://docs.pytest.org/en/latest/getting-started.html 官网推荐的plugin https://docs.pytest.org/ ...
- C#创建日志方法
1.通过流的方式 public void WriteLog(string log) { StreamWriter stream; string path = "D:/WriteLog/&qu ...
- 【转】Jython安装(Win)
ython的安装比较简单,Jython的安装程序本身就是一个Java应用程序,因此,在安装之前,你必须具备Java运行的环境.下面以 Jython的Jython2.2.1为例,说明Jython的安装步 ...
- org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.trs.om.bean.User.retryCount
六月 29, 2019 5:42:45 下午 org.apache.catalina.core.AprLifecycleListener init信息: The APR based Apache To ...
- 编程语言分类及python所属类型
编程语言分类及python所属类型 编程语言主要从以下几个角度为进行分类:编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言. 编译和解释的区别是什么? 编译器是把源程序的每一条语句都 ...
- 基于matlab的傅里叶变换
原文出处https://blog.csdn.net/qq_37366291/article/details/79832886 例子1 作用:使用傅里叶变换找出隐藏在噪声中的信号的频率成分.(指定信号的 ...
- Python学习之路15☞socket编程
一 客户端/服务器架构 即C/S架构,包括 1.硬件C/S架构(打印机) 2.软件C/S架构(web服务) C/S架构与socket的关系: 我们学习socket就是为了完成C/S架构的开发 二 os ...
- 【NS2】Installing ns-2.29 in Ubuntu 12.04
Installing ns-2.29 in Ubuntu 12.04 Off late, we try to use(install) a old software in a new Oper ...
- VS开发ArcEngine时的一个异常信息——“ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components.”
问题描述:程序报错“ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS ...
- python 内置函数补充 or 递归 or 二分法
一.内置函数的补充 repr() 显示出字符串的官方表示形式 chr() print(chr(20013)) # 把数字编码转换成字符串 ord() print(ord('中')) # 20013 把 ...