增加单个 socket.send() 调用中发送的数据量
我们有类似于下面的代码,用于在计算机和远程主机之间创建启用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
send
方法不保证发送所有字节,但sendall
方法可以。这将避免字符串切片。The
send
method doesn't guarantee to send all the bytes, but thesendall
method does. That will avoid the string slicing.即使可以做到,将块大小从 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).