- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在研究 Python 中的套接字,我希望能够将稀疏图像文件从一台机器发送到另一台机器。正如预期的那样,通过 python 套接字发送稀疏文件不会保留文件的稀疏性。我想做一个稀疏的 tar 并以这种方式发送,但我就是想不通。
tarfile 模块说它支持读取 GNU 格式的稀疏文件,这对我创建它们没有帮助……但是 python 文档说 Pax 格式“几乎没有限制”。我不确定这是否意味着我可以创建存档并保留稀疏文件或不使用 pax 格式...我一直在尝试,但我只是不知道它如何工作。
如果这个解决方案不是一个选项,是否有任何其他方法可以通过套接字发送稀疏文件?我讨厌通过我的应用程序的系统命令调用“tar -xSf”...
谢谢,
服务器
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.bind((socket.gethostname(), 50001))
s.listen(1)
img = open('test.img', 'rb')
client, addr = s.accept()
l = img.read(8192)
while(l):
client.send(l)
l = img.read(8192)
img.close()
s.close()
客户端
host = ''
port = 50001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.connect((host, port))
img = open('./newimg.img', 'wb')
l = s.recv(8192)
while(l):
img.write(l)
l = s.recv(8192)
img.close()
s.close()
在服务器上,我创建了一个新的稀疏文件:truncate -s 1G test.img
a du -h 显示:0 test.img
我运行我的服务器和客户端。这是传输文件的 du -h:1.0G newimg.img
如您所见,它扩展了文件并且不再稀疏。
最佳答案
如果您写入文件的开头,查找到结尾并在那里写入,通常会在文件中创建空洞。如果您读取文件,即使文件中有漏洞,您也正在读取零。当您发送文件时,文字字节会被发送,当然也会被读取。当您随后写入字节时,所有字节都将被写入,并且不会发生文件系统创建漏洞的情况。
为了缓解这种情况,您可以先寻找文件中的漏洞,将漏洞发送到漏洞所在的位置,然后再发送文件的其余部分。
以下内容未经完善,但应该能为您提供一个起点。
import os
f = open(path, "b")
fd = f.fileno()
end = os.stat(fd).st_size
holes = []
offset = os.lseek(fd, 0, os.SEEK_HOLE)
while offset != end:
end_hole = os.lseek(fd, offset, os.SEEK_DATA)
holes.append((offset, end_hole))
offset = end_hole
[open socket and stuff]
# send the holes
socket.write(json.dumps(holes)) # encode appropriately
# send file
f.seek(0)
total = 0
for hole in holes:
while total < hole[0]:
l = f.read(8192)
if len(l) + total > hole[0]:
socket.write(l[:len(l) + total - hole[0]])
l.seek(hole[1])
total += len(1) + total - hole[0]
else:
socket.write(l)
total += len(l)
然后在客户端:
still_json = True
a = []
l = s.recv(8192)
while(still_json):
a.append(l)
if check_json_end(l):
still_json = False
else:
l = s.recv(8192)
holes = parse_json(a) # the last chunk can contain something that is not json
# I asume that a still contains the bytes that are not json
fout = open(outfile, "wb")
total = 0
fout.write(a[0]) # handle the case where the first rest after the json in a is already after a hole
total += len(a[0])
for hole in holes:
while total < hole[0]:
l = socket.recv(8192)
if len(l) + total > hole[0]:
fout.write(l[:len(l) + total - hole[0]])
fout.seek(hole[1])
fout.write(l[len(l) + total - hole[0]:])
else:
fout.write(l)
total += len(l)
其中可能有很多错误,你应该重新考虑每一行,但总的原则应该没问题。 JSON 当然是任意选择的,在这种情况下可能还有其他更好的协议(protocol)。您也可以创建自己的。
关于python - 发送稀疏文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38227854/
我在服务器上 checkout 了一个 git 存储库。该存储库过去在根目录下包含所有相关文件,但我必须进行一些更改,现在我有两个文件夹,src 和 dist,我想跟踪这两个文件夹. 我遇到的问题是,
我很难弄清楚 VkDescriptorSetLayoutBinding::binding 的任何用例,这是结构: struct VkDescriptorSetLayoutBinding { u
Python中能否有效获取稀疏向量的范数? 我尝试了以下方法: from scipy import sparse from numpy.linalg import norm vector1 = spa
我正在尝试找出为什么这段代码不对数组进行排序... 任意向量。 x = array([[3, 2, 4, 5, 7, 4, 3, 4, 3, 3, 1, 4, 6, 3, 2, 4, 3, 2]])
有谁知道如何压缩(编码)稀疏 vector ?稀疏 vector 表示有许多“0”的 1xN 矩阵。 例如 10000000000001110000000000000000100000000 上面是稀
我使用稀疏高斯过程进行 Rasmussen 回归。[http://www.tsc.uc3m.es/~miguel/downloads.php][1] 预测平均值的语法是: [~, mu_1, ~, ~
我在朴素贝叶斯分类器中使用 Mahout API。其中一个功能是 SparseVectorsFromSequenceFiles虽然我已经尝试过旧的谷歌搜索,但我仍然不明白什么是稀疏 vector 。最
我正在尝试将JavaScript稀疏数组映射到C#表示形式。 建议这样做的方法是什么? 它正在考虑使用一个字典,该字典包含在原始数组中包含值的原始词列表。 还有其他想法吗? 谢谢! 最佳答案 注意 针
如果我想求解一个完整上三角系统,我可以调用linsolve(A,b,'UT')。然而,这目前不支持稀疏矩阵。我该如何克服这个问题? 最佳答案 UT 和 LT 系统是最容易解决的系统之一。读一读on t
我有一个带有 MultiIndex 的 Pandas DataFrame。 MultiIndex 的值在 (0,0) 到 (1000,1000) 范围内,该列有两个字段 p 和 q. 但是,DataF
我目前正在实现一个小型有限元模拟。使用 Python/Numpy,我正在寻找一种有效的方法来创建全局刚度矩阵: 1)我认为应该使用coo_matrix()从较小的单元刚度矩阵创建稀疏矩阵。但是,我可以
a , b是 1D numpy ndarray与整数数据类型具有相同的大小。 C是一个 2D scipy.sparse.lil_matrix . 如果索引[a, b]包含重复索引,C[a, b] +=
我有一个大的、连通的、稀疏的邻接表形式的图。我想找到两个尽可能远的顶点,即 diameter of the graph以及实现它的两个顶点。 对于不同的应用程序,我对无向和有向情况下的这个问题都很感兴
上下文:我将 Eigen 用于人工神经网络,其中典型维度为每层约 1000 个节点。所以大部分操作是将大小为 ~(1000,1000) 的矩阵 M 与大小为 1000 的 vector 或一批 B v
我有一些大小合适的矩阵 (2000*2000),我希望在矩阵的元素中有符号表达式 - 即 .9**b + .8**b + .7**b ... 是一个元素的例子。矩阵非常稀疏。 我通过添加中间计算来创建
在 R 或 C++ 中是否有一种快速填充(稀疏)矩阵的方法: A, B, 0, 0, 0 C, A, B, 0, 0 0, C, A, B, 0 0, 0, C, A, B 0, 0, 0, C, A
我有一个大的稀疏 numpy/scipy 矩阵,其中每一行对应于高维空间中的一个点。我想进行以下类型的查询: 给定一个点P(矩阵中的一行)和一个距离epsilon,找到与epsilon距离最大的所有点
假设我有一个 scipy.sparse.csr_matrix 代表下面的值 [[0 0 1 2 0 3 0 4] [1 0 0 2 0 3 4 0]] 我想就地计算非零值的累积和,这会将数组更改为:
我了解如何在 Git 中配置稀疏 checkout ,但我想知道是否可以消除前导目录。例如,假设我有一个 Git 存储库,其文件夹结构如下: 文件夹1/foo 文件夹2/foo/bar/stuff 文
根据 this thread , Git 中的排除 sparse-checkout feature应该实现。是吗? 假设我有以下结构: papers/ papers/... presentations
我是一名优秀的程序员,十分优秀!