- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的 Windows PC 上有一个用 python 编写的简单服务器,它从目录读取文件,然后通过 TCP 将文件发送到客户端。
客户端正确接收 HTML 和 Javascript 等文件(发送的文件与原始文件匹配)。
问题是图像数据被截断。
奇怪的是,不同的图像被截断为不同的长度,但每张图像都是一致的。
例如,特定的 1MB JPG 始终以 95 字节接收。另一个应该为 7KB 的图像被接收为 120 字节。
用notepad++打开截断后的图片文件,里面的数据是正确的。 (唯一的问题是文件结束得太快了)。
我没有看到文件结束位置的模式。截断前后的字符/字节因图像而异。
我尝试了三种不同的方式让服务器读取文件,但它们的结果都是一样的。
下面是读取和发送文件的片段:
print ("Cache size=" + str(os.stat(filename).st_size))
#1st attempt, using readlines
fileobj = open(filename, "r")
cacheBuffer = fileobj.readlines()
for i in range(0, len(cacheBuffer)):
tcpCliSock.send(cacheBuffer[i])
#2nd attempt, using line, same result
with open(filename) as f:
for line in f:
tcpCliSock.send(f)
#3rd attempt, using f.read(), same result
with open(filename) as f:
tcpCliSock.send(f.read())
脚本将读取的文件大小打印到控制台,字节数与原始图像匹配。所以这证明问题出在发送上,对吧?
如果问题出在发送上,我可以更改什么以正确发送整个图像?
最佳答案
由于您处理的是二进制文件的图像,因此您需要以二进制 模式打开文件。
open(filename, 'rb')
来自 open()
的 Python 文档:
The default is to use text mode, which may convert
'\n'
characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append'b'
to the mode value to open the file in binary mode, which will improve portability. (Appending'b'
is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.)
由于您的服务器在 Windows 上运行,因此当您读取
文件时,Python 会将它看到的每个 \r\n
转换为 \n
。对于文本文件,这很好:您可以编写与平台无关的代码,只处理 \n
字符。对于二进制文件,这会完全破坏您的数据。这就是为什么在处理二进制文件时使用 'b'
很重要,但在处理文本文件时不要使用它也很重要。
此外,由于 TCP 是一种流协议(protocol),因此最好将数据流成更小的片段到套接字中。这避免了将整个文件读入内存的需要,这将降低内存使用量。像这样:
with open(filename, 'rb') as f:
while True:
data = f.read(4096)
if len(data) == 0:
break
tcpCliSock.send(data)
关于Python 服务器正在通过 TCP 发送截断的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40072052/
我有一个 1850-2005 年月地表气温的 netCDF 文件。如何在 unix 中截断文件,以便新文件的时间维度从 1855 年到 2005 年?反之亦然,截断文件,使其改为 1850-2000?
我收到此错误 Bulk load data conversion error (truncation) for row 1, column 12 (is_download) 这是 csv...它只有一
我有一个列表,它是电子邮件正文,每行末尾都有一个日期和时间戳。邮票的格式是一致的,所以可以从右边计算表达式。以下是示例数据: Dear Volunteer2018-05-21 19:59:15 You
我正在使用内置 truncatewords_html Django 的过滤器,它在最后添加了“...”,相反,我想用“查看更多”链接替换它。 我怎样才能做到这一点? 最佳答案 最好编写自己的过滤器。您
我正在使用 SQL 加载器将我的数据加载到数据库中。 在插入数据之前,我需要删除表中的现有数据: options(skip=1,load=250000,errors=0,ROWS=30000,BIND
我正在尝试掌握消息队列的窍门。由于某种原因,当显示我在控制台中输入的消息时,字符串有时会被截断或更改。有谁知道为什么会发生这种情况? void *readFromQueue() { int r
我正在使用mediawiki API(例如http://en.wikipedia.org/w/api.php),我希望能够“截断”mysql表以便重置本地安装,同时保留一些表(用户,?...) 。SQ
想要截断一个存在的表: IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'mytable') TRUNCATE
我正在实现一个使用 Python Numpy 包的程序。我正在尝试修改数组的元素,以便我只需采用 elem[i][j] 并将其设置为 elem[i][j]/10。但是,我不断收到某种截断,其中元素在操
我有一个 64 位 long int,其中包含一些位域。我需要将存储在第二个和第三个字节中的 16 位带符号整数添加到一个 32 位值中。我正在使用这样的东西: u32 Function( s32 v
我有这样的文字:“我的文字是 blabla blabla, lala lala”。 我希望在我的 UILabel 中有这样的文本:“My text is ...lala”。 如何配置我的 UILabe
Umbraco Truncate 似乎不适合我,当我使用正确的代码(根据互联网)时,它会不断出错。我不知道它有什么问题。 错误代码: Compiler Error Message: CS1502: T
早些时候,我是使用 JS 动态地完成它的。但是我们遇到了一些性能问题,因为我们必须提供一个替代选项。 我现在使用文本溢出样式截断选项卡名称上的长文本。 但我有一个小问题,如果有人能解决的话 目前这是我
使用Rspec时截断、事务和删除数据库策略有什么区别?我找不到任何资源来解释这一点。我阅读了 Database Cleaner 自述文件,但它没有解释它们各自的作用。 为什么我们必须对 capybar
当然, 尚有诸位前辈也曾把以上三种方案结合一二, 以达到更广泛的适应度. 不过, 这厢先前在网路上搜索许久, 却未曾寻到三种方案合为一体的尝试, 于是只好自己动手写一下了:) 没有dem
有没有办法截断HSQLDB中的所有表? 如果这不可能,是否有任何方法可以级联删除具有外键引用的表? 最佳答案 可以截断模式中的所有表: 截断模式并提交 此命令有一些选项在指南中有描述: http://
我有一个要截断的时间戳。我正在使用 trunc oracle中的函数。这似乎做了我想要的但是从文档中它应该只接受日期而不是时间戳 select TRUNC(TO_DATE('22-AUG-13'),
我读到一旦你耗尽了一个节点,你就可以删除文件然后重新启动。它工作正常, 但我只是通过排空所有节点,关闭整个集群,删除文件并重新启动来尝试它。 如果我一次只重启一个节点会怎样?据我了解有风险 重新启动的
我想截断 d3 中超过预定义限制的文本。 我不知道该怎么做。 这是我现在所拥有的: node.append("text") .attr("dx", 20) .attr("dy", ".20
嗨 Guyz 我有一个固定宽度的 WPF TextBlock 说 100 ,如果字符串不适合宽度,则最后一个字符总是被截断,因为所有字符的大小都不相同。我不想剪切字符而是我想从那里跳过文本并只显示没有
我是一名优秀的程序员,十分优秀!