增加单个 socket.send() 调用中发送的数据量

发布于 2024-12-27 12:51:33 字数 602 浏览 1 评论 0原文

我们有类似于下面的代码,用于在计算机和远程主机之间创建启用 SSL 的套接字以传输数据。我们注意到每次调用 sock.send 发送的数据量约为 16K。有没有办法配置这个?以16K块的形式发送10MB的数据非常慢,我们可以将块大小配置为1-2MB左右吗?

from OpenSSL import SSL
import socket

''' Create SSL context.
ctx = ...

''' Create SSL enabled socket.
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))

''' Connect to the remote host.
sock.connect((host_address, port))

''' msg is a large string about 10 MB.
msg = ... 

''' Send data over the socket.
total_sent = 0
while (total_sent < len(msg)):
  sent = sock.send(msg[total_sent:])
  total_sent += sent

We have code similar to the one below to create an SSL enabled socket between a machine and a remote host to transfer data. We notice that the amount of data sent in each call to sock.send is about 16K. Is there a way to configure this? Send 10 MB of data in 16K chunks is very slow, can we configure the chunk size to about 1-2 MB?

from OpenSSL import SSL
import socket

''' Create SSL context.
ctx = ...

''' Create SSL enabled socket.
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))

''' Connect to the remote host.
sock.connect((host_address, port))

''' msg is a large string about 10 MB.
msg = ... 

''' Send data over the socket.
total_sent = 0
while (total_sent < len(msg)):
  sent = sock.send(msg[total_sent:])
  total_sent += sent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2025-01-03 12:51:33

send 方法不保证发送所有字节,但 sendall 方法可以。这将避免字符串切片。

The send method doesn't guarantee to send all the bytes, but the sendall method does. That will avoid the string slicing.

千笙结 2025-01-03 12:51:33

即使可以做到,将块大小从 16K 增加到 1-2MB 几乎肯定不会对性能产生影响。可能的瓶颈是 TCP/IP 连接的吞吐量(以太网的 MTU 为可能大约 1500 或 9000 字节,所以无论如何,所有内容都会被切成那个大小的片段)。

Even if it can be done, increasing the block size from 16K to 1-2MB will almost certainly have no effect on performance. The likely bottleneck is the throughput of your TCP/IP connection (and the MTU of your Ethernet network is probably around 1500 or 9000 bytes, so everything gets chopped up into pieces of that size anyway).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文