According to the docs, Python's socket.send()
:
Returns the number of bytes sent.
However, when I use it, it seems to return the byte that was last sent, not the total number of bytes sent. Consider this code:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('localhost', 12345))
>>> r = s.send(bytes(1))
>>> print(r)
1
>>> r = s.send(bytes(15))
>>> print(r)
15
>>> r = s.send(bytes(150))
>>> print(r)
150
>>> r = s.send(bytes(255))
>>> print(r)
255
>>> r = s.send(bytes(0))
>>> print(r)
0
This SO post seems related, though it doesn't answer my question.
What I'm expecting to see is r == 1
for every line.
Solved:
I got it. This works:
>>> y = bytearray(1)
>>> y[0] = 125
>>> r = s.send(y)
>>> print(r)
1
The reason I put bytes()
in there to begin with is because without it you get:
>>> r = s.send(125)
TypeError: a bytes-like object is required, not 'int'