Hi all, I am already familiar with python and socket usage and can send strings of text over these. But how would i go about sending, say, an MP3 file?
+1
A:
The following code would do what you literally ask (assuming thesocket
is a connected stream socket):
with open('thefile.mp3', 'rb') as f:
thesocket.sendall(f.read())
but of course it's unlikely to be much use without some higher-level protocol to help the counterpart know how much data it's going to receive, what type of data, and so forth.
Alex Martelli
2010-06-03 21:50:48
You probably want to split that up so that not the whole file is read into memory at once.
Marian
2010-06-03 21:53:46
I see. Thanks alot. This was all i needed! :)
Jake
2010-06-03 21:57:23
@Jake, consider accepting the answer (by clicking on the checkmark-shaped icon to the left of the answer) -- accepting the answer to you question that has most helped yoi is crucial SO etiquette (and as such is rewarded by giving you a few extra rep points, and a few more to me;-).
Alex Martelli
2010-06-04 00:39:21
No problem. Thanks for the tip. I am quite new here :)
Jake
2010-06-07 15:25:55