python套接字在逐行调试时工作正常,但在完整运行时无法工作[重复]

2024-01-10

我正在开发一个项目,该项目涉及传输文件,并为文件的每个块进行 CRC 校验和计算,例如此处为 40960 字节。我的问题是,当我逐行调试代码时,一切正常,但当我完全运行代码时,我在接收器端得到不同的 CRC 校验和。我猜问题出在缓冲区上。

问题出在发件人这边。

任何帮助将不胜感激,请让我知道如何完成这项任务。

谢谢。

发送方:

import socket
import tqdm
import os
import crcmod

SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 40960 # bytes

host = "10.218.105.192"
port = 5001

crc32 = crcmod.mkCrcFun(0x104c11db7, 0, False, 0xFFFFFFFF)

s = socket.socket()
print(f"[+] Connecting to {host}:{port}")
s.connect((host, port))
print("[+] Connected.")

#filename = input('Enter file name --> ')
filename="testfile"
if not os.path.exists(filename):
    print("file-doesn't-exist")
    s.close()
filesize = os.path.getsize(filename)
crc_of_file = open(filename, 'rb')
crc_d = crc_of_file.read()
crc = crc32(crc_d)
# send the filename and filesize
#s.send(f"{filename}{SEPARATOR}{filesize}{SEPARATOR}{crc}".encode())
# start sending the file
chunk_crc=[]
progress = tqdm.tqdm(range(filesize), f"Sending {filename}", unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "rb") as f:
    while True:
        # read the bytes from the file
        bytes_read = f.read(BUFFER_SIZE)
        if not bytes_read:
            # file transmitting is done
            break
        bytes_read += crc32(bytes_read).to_bytes(4, 'big')# we use sendall to assure transimission in
        # busy networks
        #s.send(f"{this_chunk_crc}\n".encode())
        s.sendall(bytes_read)
        # update the progress bar
        progress.update(len(bytes_read))
# close the socket
s.close()

接收方:

import socket
import tqdm
import os
import crcmod
# device's IP address
SERVER_HOST = "0.0.0.0"
SERVER_PORT = 5001
# receive 40964 bytes each time which includes CRC checksum
BUFFER_SIZE = 40964


SEPARATOR = "<SEPARATOR>"
s = socket.socket()
s.bind((SERVER_HOST, SERVER_PORT))
s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")

client_socket, address = s.accept()
# if below code is executed, that means the sender is connected
print(f"[+] {address} is connected.")
# receive the file infos
# receive using client socket, not server socket
#received = client_socket.recv(BUFFER_SIZE).decode()
filename="testfile"
filesize=int("2367275")
#filename, filesize,crc = received.split(SEPARATOR)
# remove absolute path if there is
filename = os.path.basename(filename)
# convert to integer
filesize = int(filesize)
#print(f"[+] File CRC is {crc}")


crc32 = crcmod.mkCrcFun(0x104c11db7, 0, False, 0xFFFFFFFF)


# start receiving the file from the socket
# and writing to the file stream
progress = tqdm.tqdm(range(filesize), f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)
#with open(filename, "wb") as f:
i=0
while True:
    # read bytes from the socket (receive)
    bytes_read = client_socket.recv(BUFFER_SIZE)
    if not bytes_read:
        # nothing is received
        # file transmitting is done
        break
    # write to the file the bytes we just received
    if (len(bytes_read) >= BUFFER_SIZE) :
        this_chunk_crc = crc32(bytes_read)
        print(this_chunk_crc)
        #partname=filename+"Part"+str(i)+" "+str(this_chunk_crc)
        with open(filename, "wb") as f:
            #bytes_read -= crc32(bytes_read).to_bytes(4, 'big')
            f.write(bytes_read)
    progress.update(len(bytes_read))


# close the client socket
client_socket.close()
# close the server socket
s.close()

正确的输出应该类似于(如您所见,每一步的 CRC 定义常数应为 955982468,并且与文件无关)


Receiving testfile:   0%|          | 0.00/2.26M [00:00<?, ?B/s]955982468
Receiving testfile:   2%|▏         | 40.0k/2.26M [00:21<20:26, 1.90kB/s]955982468
Receiving testfile:   3%|▎         | 80.0k/2.26M [00:22<08:57, 4.26kB/s]955982468
Receiving testfile:   5%|▌         | 120k/2.26M [00:23<04:58, 7.52kB/s] 955982468
Receiving testfile:   7%|▋         | 160k/2.26M [00:23<03:08, 11.7kB/s]955982468
Receiving testfile:   9%|▊         | 200k/2.26M [00:24<02:08, 16.9kB/s]955982468
Receiving testfile:  10%|█         | 240k/2.26M [00:24<01:31, 23.2kB/s]955982468
Receiving testfile:  12%|█▏        | 280k/2.26M [00:25<01:08, 30.2kB/s]955982468
Receiving testfile:  14%|█▍        | 320k/2.26M [00:26<01:02, 32.9kB/s]955982468
Receiving testfile:  16%|█▌        | 360k/2.26M [00:26<00:49, 40.4kB/s]955982468
Receiving testfile:  17%|█▋        | 400k/2.26M [00:27<00:40, 47.9kB/s]955982468
Receiving testfile:  19%|█▉        | 440k/2.26M [00:27<00:35, 53.9kB/s]955982468
Receiving testfile:  21%|██        | 480k/2.26M [00:28<00:30, 60.5kB/s]955982468
Receiving testfile:  22%|██▏       | 520k/2.26M [00:28<00:27, 66.2kB/s]955982468
Receiving testfile:  24%|██▍       | 560k/2.26M [00:29<00:25, 70.3kB/s]955982468
Receiving testfile:  26%|██▌       | 600k/2.26M [00:30<00:26, 67.2kB/s]955982468
Receiving testfile:  28%|██▊       | 640k/2.26M [01:01<06:52, 4.15kB/s]955982468
Receiving testfile:  29%|██▉       | 680k/2.26M [01:01<04:45, 5.85kB/s]955982468
Receiving testfile:  31%|███       | 720k/2.26M [01:02<03:19, 8.17kB/s]955982468
Receiving testfile:  33%|███▎      | 760k/2.26M [01:02<02:23, 11.1kB/s]955982468
Receiving testfile:  35%|███▍      | 800k/2.26M [01:03<01:43, 15.0kB/s]955982468
Receiving testfile:  36%|███▋      | 840k/2.26M [01:03<01:15, 19.8kB/s]955982468
Receiving testfile:  38%|███▊      | 880k/2.26M [01:04<01:01, 23.7kB/s]955982468
Receiving testfile:  40%|███▉      | 920k/2.26M [01:05<00:47, 29.9kB/s]955982468
Receiving testfile:  42%|████▏     | 960k/2.26M [01:05<00:37, 37.2kB/s]955982468
Receiving testfile:  43%|████▎     | 0.98M/2.26M [01:06<00:30, 44.5kB/s]955982468
Receiving testfile:  45%|████▍     | 1.02M/2.26M [01:06<00:26, 48.7kB/s]955982468
Receiving testfile:  47%|████▋     | 1.05M/2.26M [01:07<00:22, 55.8kB/s]955982468
Receiving testfile:  48%|████▊     | 1.09M/2.26M [01:07<00:18, 65.4kB/s]955982468
Receiving testfile:  50%|█████     | 1.13M/2.26M [01:08<00:17, 68.5kB/s]955982468
Receiving testfile:  52%|█████▏    | 1.17M/2.26M [01:08<00:15, 71.5kB/s]955982468
Receiving testfile:  54%|█████▎    | 1.21M/2.26M [01:09<00:15, 72.5kB/s]955982468
Receiving testfile:  55%|█████▌    | 1.25M/2.26M [01:09<00:14, 73.8kB/s]955982468
Receiving testfile:  57%|█████▋    | 1.29M/2.26M [01:10<00:13, 76.1kB/s]955982468
Receiving testfile:  59%|█████▉    | 1.33M/2.26M [01:10<00:12, 76.4kB/s]955982468
Receiving testfile:  61%|██████    | 1.37M/2.26M [01:11<00:12, 76.6kB/s]955982468
Receiving testfile:  62%|██████▏   | 1.41M/2.26M [01:11<00:11, 77.4kB/s]955982468
Receiving testfile:  64%|██████▍   | 1.45M/2.26M [01:12<00:11, 76.6kB/s]955982468
Receiving testfile:  66%|██████▌   | 1.48M/2.26M [01:12<00:09, 84.2kB/s]955982468
Receiving testfile:  67%|██████▋   | 1.52M/2.26M [01:13<00:09, 81.2kB/s]955982468
Receiving testfile:  69%|██████▉   | 1.56M/2.26M [01:13<00:09, 78.5kB/s]955982468
Receiving testfile:  71%|███████   | 1.60M/2.26M [01:14<00:08, 77.3kB/s]955982468
Receiving testfile:  73%|███████▎  | 1.64M/2.26M [01:15<00:08, 76.2kB/s]955982468
Receiving testfile:  74%|███████▍  | 1.68M/2.26M [01:15<00:07, 85.2kB/s]955982468
Receiving testfile:  76%|███████▌  | 1.72M/2.26M [01:15<00:06, 81.0kB/s]955982468
Receiving testfile:  78%|███████▊  | 1.76M/2.26M [01:16<00:05, 87.8kB/s]955982468
Receiving testfile:  80%|███████▉  | 1.80M/2.26M [01:16<00:05, 84.3kB/s]955982468
Receiving testfile:  81%|████████▏ | 1.84M/2.26M [01:17<00:05, 80.5kB/s]955982468
Receiving testfile:  83%|████████▎ | 1.88M/2.26M [01:17<00:05, 78.7kB/s]955982468
Receiving testfile:  85%|████████▍ | 1.91M/2.26M [01:18<00:04, 79.6kB/s]955982468
Receiving testfile:  87%|████████▋ | 1.95M/2.26M [01:18<00:03, 84.7kB/s]955982468
Receiving testfile:  88%|████████▊ | 1.99M/2.26M [01:19<00:03, 79.6kB/s]955982468
Receiving testfile:  90%|████████▉ | 2.03M/2.26M [01:20<00:03, 76.8kB/s]955982468
Receiving testfile:  92%|█████████▏| 2.07M/2.26M [01:20<00:02, 81.9kB/s]955982468
Receiving testfile:  93%|█████████▎| 2.11M/2.26M [01:21<00:02, 62.6kB/s]955982468
Receiving testfile:  95%|█████████▌| 2.15M/2.26M [01:22<00:01, 63.4kB/s]955982468
Receiving testfile:  97%|█████████▋| 2.19M/2.26M [01:22<00:01, 70.8kB/s]955982468
Receiving testfile:  99%|█████████▊| 2.23M/2.26M [01:23<00:00, 69.7kB/s]955982468
Receiving testfile: 2.26MB [02:00, 19.7kB/s]

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python套接字在逐行调试时工作正常,但在完整运行时无法工作[重复] 的相关文章

  • 如何让python优雅地失败?

    我只是想知道如何让 python 在所有可能的错误中以用户定义的方式失败 例如 我正在编写一个处理 大 项目列表的程序 并且某些项目可能不符合我定义的格式 如果 python 检测到错误 它目前只会输出一条丑陋的错误消息并停止整个过程 但是
  • 以矢量化方式在另一个 DataFrame 中查找包含值子集的行

    如何匹配此 DataFrame 中的值source car id lat lon 0 100 10 0 15 0 1 100 12 0 10 0 2 100 09 0 08 0 3 110 23 0 12 0 4 110 18 0 32 0
  • 通过 SocketCAN 进行 boost::asio

    我正在考虑利用升压阿西奥 http www boost org doc libs 1 49 0 doc html boost asio html从a读取数据套接字CAN http en wikipedia org wiki SocketCA
  • 最小二乘法拟合直线 python 代码

    我有一个由 X 和 Y 坐标组成的散点图 我想使用直线的最小二乘拟合来获得最佳拟合线 直线最小二乘拟合是指 如果 x 1 y 1 x n y n 是测量数据对 则最佳直线是y A Bx 这是我的Python代码 number of poin
  • matplotlib 中的 R 风格数据轴缓冲区

    R 绘图自动设置 x 和 y 限制 以在数据和轴之间留出一些空间 我想知道 matplotlib 是否有办法自动执行相同的操作 如果没有 是否有一个好的公式或 经验法则 来说明 R 如何设置其轴限制 在 matplotlib 中 您可以通过
  • 组和平均 NumPy 矩阵

    假设我有一个任意的 numpy 矩阵 如下所示 arr 6 0 12 0 1 0 7 0 9 0 1 0 8 0 7 0 1 0 4 0 3 0 2 0 6 0 1 0 2 0 2 0 5 0 2 0 9 0 4 0 3 0 2 0 1 0
  • Python 的 mysqldb 晦涩文档

    Python 模块 mysqldb 中有许多转义函数 我不理解它们的文档 而且我努力查找它们也没有发现任何结果 gt gt gt print mysql escape doc escape obj dict escape any speci
  • Perl 是否有相当于 Python 的 `if __name__ == '__main__'` 的功能?

    有没有一种方法可以确定当前文件是否是 Perl 源中正在执行的文件 在 Python 中 我们使用以下结构来做到这一点 if name main This file is being executed raise NotImplemente
  • 使用多级解决方案计算二维网格中的最近邻

    我有一个问题 在 x y 大小的网格中 我提供了一个点 并且我需要找到最近的邻居 在实践中 我试图在 pygame 中找到距离光标最近的点 该点跨越颜色距离阈值 计算如下 sqrt rgb1 0 rgb2 0 2 rgb1 1 rgb2 1
  • 如何使用 i18n 切换器将“LANGUAGE_CODE”保存到数据库,以便在 Django 中的不同浏览器中语言不会更改?

    有什么办法可以改变它的值LANGUAGE CODE单击按钮 发送请求 时 settings py 中的变量会动态变化吗 我希望用户设置自己的 默认语言 他们的帐户 现在 用户可以使用下拉列表选择他们的首选语言 并且网站会得到完美的翻译 并且
  • 为什么 __instancecheck__ 没有被调用?

    我有以下 python3 代码 class BaseTypeClass type def new cls name bases namespace kwd result type new cls name bases namespace p
  • 用于多个窗口的 Tkinter 示例代码,为什么按钮无法正确加载?

    我正在编写一个程序 应该 按一下按钮即可打开一个窗口 按另一个按钮关闭新打开的窗口 我使用类 以便稍后可以将代码插入到更大的程序中 但是 我无法正确加载按钮 import tkinter as tk class Demo1 tk Frame
  • Jupyter Notebook 中的深色模式绘图 - Python

    我正在使用 Jupyter Notebook 目前正在使用 JupyterThemes 的深色日光主题 我注意到我的绘图不是处于黑暗模式 并且文本仍然是黑色并且在日光照射的背景上无法读取 JupyterThemes 的自述文件建议在 ipy
  • 根据列索引重命名 Dataframe 列

    是否有内置函数可以按索引重命名 pandas 数据框 我以为我知道列标题的名称 但事实证明第二列中有一些十六进制字符 根据我接收数据的方式 我将来可能会在第 2 列中遇到这个问题 因此我无法将这些特定的十六进制字符硬编码到 datafram
  • 在python中读取PASCAL VOC注释

    我在 xml 文件中有注释 例如这个 它遵循 PASCAL VOC 约定
  • 更换壳牌管道[重复]

    这个问题在这里已经有答案了 在 subprocess 模块的 Python 2 7 文档中 我找到了以下片段 p1 Popen dmesg stdout PIPE p2 Popen grep hda stdin p1 stdout stdo
  • Python:无法使用 os.system() 打开文件

    我正在编写一个使用该应用程序的 Python 脚本pdftk http www pdflabs com tools pdftk the pdf toolkit 几次来执行某些操作 例如 我可以在 Windows 命令行 shell 中使用
  • Streamlabs API 405 响应代码

    我正在尝试使用Streamlabs API https dev streamlabs com Streamlabs API 使用 Oauth2 来创建应用程序 因此 首先我将使用我的应用程序的用户发送到一个授权链接 其中包含我的应用程序的客
  • 在父类中访问子类变量

    我有一个父类和一个继承的子类 我想知道如何访问我的父类中的子类变量 我尝试了这个但失败了 class Parent object def init self print x class Child Parent x 1 x Child Er
  • 使用 SERVER_NAME 时出现 Flask 404

    在我的 Flask 配置中 我将 SERVER NAME 设置为 app example com 之类的域 我这样做是因为我需要使用url for with external网址 如果未设置 SERVER NAME Flask 会认为服务器

随机推荐