If you are using a datagram socket (e. g. the UDP protocol over IP), the Socket API guarantees that if your n
is less than the maximum payload size, then your data will be sent in a single packet. So just calling socket.send
would be sufficient.
The easiest way to send data over a stream socket is to use the socket.sendall
method, as send
for streams doesn't guarantee that all the data is actually sent (and you should repeatedly call send
in order to transmit all the data you have). Here is an example:
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('localhost', 12345))
>>> data = 'your data of length n'
>>> s.sendall(data)
As @Alex has already mentioned, there is nothing related to some kind of "primitives" in Python. It is just and issue with the Socket API.