Increasing the amount of data sent in a single socket.send() call

462 Views Asked by At

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
2

There are 2 best solutions below

5
On

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).

0
On

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