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套接字在逐行调试时工作正常,但在完整运行时无法工作[重复] 的相关文章

  • 类的 IPython 表示

    我正在使用我创建的模块尝试 IPython 但它没有显示类对象的实际表示 相反 它显示类似的内容 TheClass module TheClass name I heavily在这个模块中使用元类 我有真正有意义的类表示 应该向用户显示 是
  • Pandas set_levels,如何避免标签排序?

    我使用时遇到问题set levels多索引 from io import StringIO txt Name Height Age Metres A 1 25 B 95 1 df pd read csv StringIO txt heade
  • 为什么 dataclasses.astuple 返回类属性的深层副本?

    在下面的代码中astuple函数正在执行数据类的类属性的深层复制 为什么它不能产生与函数相同的结果my tuple import copy import dataclasses dataclasses dataclass class Dem
  • matplotlib 图中点的标签

    所以这是一个关于已发布的解决方案的问题 我试图在我拥有的 matplotlib 散点图中的点上放置一些数据标签 我试图在这里模仿解决方案 是否有与 MATLAB 的 datacursormode 等效的 matplotlib https s
  • 嵌套列表的重叠会产生不必要的间隙

    我有一个包含三个列表的嵌套 这些列表由 for 循环填充 并且填充由 if 条件控制 第一次迭代后 它可能类似于以下示例 a 1 2 0 0 0 0 0 0 4 5 0 0 0 0 0 0 6 7 根据条件 它们不重叠 在第二次迭代之后 新
  • 使用主题交换运行多个 Celery 任务

    我正在用 Celery 替换一些自制代码 但很难复制当前的行为 我期望的行为如下 创建新用户时 应向tasks与交换user created路由键 该消息应该触发两个 Celery 任务 即send user activate email
  • 从Django中具有外键关系的两个表中检索数据? [复制]

    这个问题在这里已经有答案了 This is my models py file from django db import models class Author models Model first name models CharFie
  • PyQt 使用 ctrl+Enter 触发按钮

    我正在尝试在我的应用程序中触发 确定 按钮 我当前尝试的代码是这样的 self okPushButton setShortcut ctrl Enter 然而 它不起作用 这是有道理的 我尝试查找一些按键序列here http ftp ics
  • GUI(输入和输出矩阵)?

    我需要创建一个 GUI 将数据输入到矩阵或表格中并读取此表单数据 完美的解决方案是限制输入表单仅允许float 例如 A 1 02 0 25 0 30 0 515 0 41 1 13 0 15 1 555 0 25 0 14 1 21 2
  • 打印包含字符串和其他 2 个变量的变量

    var a 8 var b 3 var c hello my name is var a and var b bye print var c 当我运行程序时 var c 会像这样打印出来 hello my name is 8 and 3 b
  • 使用 Python Oauthlib 通过服务帐户验证 Google API

    我不想使用适用于 Python 的 Google API 客户端库 但仍想使用 Python 访问 Google APIOauthlib https github com idan oauthlib 创建服务帐户后谷歌开发者控制台 http
  • 嵌套作用域和 Lambda

    def funct x 4 action lambda n x n return action x funct print x 2 prints 16 我不太明白为什么2会自动分配给n n是返回的匿名函数的参数funct 完全等价的定义fu
  • 尽管我已在 python ctypes 中设置了信号处理程序,但并未调用它

    我尝试过使用 sigaction 和 ctypes 设置信号处理程序 我知道它可以与python中的信号模块一起使用 但我想尝试学习 当我向该进程发送 SIGTERM 时 但它没有调用我设置的处理程序 只打印 终止 为什么它不调用处理程序
  • pandas - 包含时间序列数据的堆积条形图

    我正在尝试使用时间序列数据在 pandas 中创建堆积条形图 DATE TYPE VOL 0 2010 01 01 Heavy 932 612903 1 2010 01 01 Light 370 612903 2 2010 01 01 Me
  • Pandas 组合不同索引的数据帧

    我有两个数据框df 1 and df 2具有不同的索引和列 但是 有一些索引和列重叠 我创建了一个数据框df索引和列的并集 因此不存在重复的索引或列 我想填写数据框df通过以下方式 for x in df index for y in df
  • Python GTK+ 画布

    我目前正在通过 PyGobject 学习 GTK 需要画布之类的东西 我已经搜索了文档 发现两个小部件似乎可以完成这项工作 GtkDrawingArea 和 GtkLayout 我需要一些基本函数 如 fillrect 或 drawline
  • Python:Goslate 翻译请求返回“503:服务不可用”[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我们不允许提出寻求书籍 工具 软件库等推荐的问题 您可以编辑问题 以便用事实和引文来回答 这个问题似乎不是关于主要由程序员使用的特定编程问
  • 如何将 Django 中的权限添加到模型并使用 shell 进行测试

    我在模型中添加了 Meta 类并同步了数据库 然后在 shell 中创建了一个对象 它返回 false 所以我真的无法理解错误在哪里或者缺少什么是否在其他文件中可能存在某种配置 class Employer User Employer in
  • 如何使用 PrimaryKeyRelatedField 更新多对多关系上的类别

    Django Rest 框架有一个主键相关字段 http www django rest framework org api guide relations primarykeyrelatedfield其中列出了我的 IDmany to m
  • pytest找不到模块[重复]

    这个问题在这里已经有答案了 我正在关注pytest 良好实践 https docs pytest org en latest explanation goodpractices html test discovery或者至少我认为我是 但是

随机推荐