First of all, your code is not actually sending 512 bytes at a time, it's receiving 512 bytes at a time.
Now, I think you're really asking two questions.
- How do you send 10 bytes at a time over a TCP connection (you are using
socket.SOCK_STREAM
)
- How do you send raw bytes using a Python socket.
Let's answer 2. first: If you call socket.send on a byte string, it should be sent out in binary as TCP payload.
For 1., the simplest approach would be to split the data into chunks (now that you know you're operating on strings, you can simply do that using the slice operations (see the Python tutorial on strings - e.g. s[0:10]
, s[10:20]
etc). Next, you need to ensure these slices are sent individually. This could be done by calling socket.send
, but the problem is, that your TCP/IP stack may group these into packets even if you don't want it to - you have after all asked it to provide you with a stream socket, socket.SOCK_STREAM
. If you were writing to a file, what you'd do in this case is you'd flush the file, but this does not appear to be easy for Python sockets (see this question).
Now, I think the answer to that question, saying it's impossible, is not quite right. It appears that Scapy will let you send 10 byte TCP chunks (I got the chunks() function from here). I checked it in wireshark and tried it multiple times with consistent results, but I didn't check the implementation to make sure this is guaranteed to happen.
You should probably ask yourself why you want to send data in chunks of 10 bytes, rather than let TCP deal with what it was designed for, and consider using delimiters.
Anyway, here's the code using Scapy (fun fact: it looks like running this does not require root privileges):
client:
from scapy.all import *
import socket
host = '192.168.0.x' #replace with your IP
my_port = 3002
message = "some string I want to send in chunks"
def chunks(lst, n):
"Yield successive n-sized chunks from lst"
for i in xrange(0, len(lst), n):
yield lst[i:i+n]
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, my_port))
ss=StreamSocket(client_socket,Raw)
for chunk in chunks(message, 10):
print "sending: " + chunk
ss.send(Raw(chunk) )
client_socket.close()
server:
import socket
my_port=3002
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", my_port))
server_socket.listen(5)
client_socket, address = server_socket.accept()
while (True):
data = client_socket.recv(512)
if (data):
print data
else:
break