开发者

How do i send a file with sockets in python?

开发者 https://www.devze.com 2023-01-02 09:49 出处:网络
I am already familiar with python and socket usage and can send strings of text over these. But how would i go ab开发者_JAVA技巧out sending, say, an MP3 file?The following code would do what you liter

I am already familiar with python and socket usage and can send strings of text over these. But how would i go ab开发者_JAVA技巧out sending, say, an MP3 file?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消