0

I just coded a little client/server program. I used the socket module of python3 for this.

And i wanted to know how to not only send strings but other datatypes besides the basic datatypes. So for example, images as jpgs or whatever or how to send mp4 or avis something like that so basically visual datatypes that are used in the daily life.

My first thought was to code it exactly the same as with strings:

socket_name.send(bytes('string'))

but then i would need a function to convert the datatype into bytes and back on the server and I couldn't find one that provides this.

So I searched for other functions and found sendfile and makefile. I tried them out but I couldn't find how to use them properly and it didn't work.

So my question is simply how to send visual data via a socket in python3.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • First, show us the code that you tried, and what went wrong with it, or where you got stuck trying to make it work. You don't really need to "convert a file to bytes"; just `open` the file in `rb` mode and then pass it to `thesocket.sendfile(f)`. On the other side, you just open a file in `wb` mode and loop over `buf = sock.recv(BUFSIZE); f.write(buf)` until you're done. The problem is: when are you done? – abarnert Apr 26 '18 at 00:22
  • If you want to just open a socket, send a single file, and close the socket, and you want the server to just save it to some randomly-named file, then you're done when the client closes the socket. But normally you're going to want to build some simple protocol. For example, send the file name, a newline, the file size (as a nice human-readable ASCII string), a newline, and then the file itself. Then the server knows what to call the file, and when it's done reading that file and ready to ask for another one. – abarnert Apr 26 '18 at 00:23
  • 1
    Anyway, you really need a tutorial on basic network protocols, and that's way too much to write in an SO question, so you'll have to search elsewhere. – abarnert Apr 26 '18 at 00:24
  • ok thanks that helped i dont know what went wrong the first time maybe just the filepath or something like this. Well for me it is enough to just simply send one file and then store it. Thank you and have a nice day. – janjanjan Apr 26 '18 at 00:27
  • ps. im new here is there a way to value ur answer? – janjanjan Apr 26 '18 at 00:28
  • 1
    Well, you can upvote a comment if you have enough rep, but it doesn’t do much. The system is designed to reward answers, not comments. But there’s really no way to write a good answer to this unless you turn it into a more specific question, like a [mcve] showing your `makefile` attempts and what went wrong with it. But if you can figure that out yourself and need to go off an experiment now, there’s really nothing to do but close the question. – abarnert Apr 26 '18 at 00:32
  • well i still have one question left i managed to send the file now (thanks to u) but how do i get the client to store it? i made it like this: with open('filename.jpg', 'w') as data: data.write(the_socket.recv(1024)) but write doesnt work here if ist not about a string i guess also do u know some detailed tutorials about networking? – janjanjan Apr 26 '18 at 00:48
  • You need to open the file in binary mode: `with open('filename.jpg', 'wb') as data:`. Text-mode files take strings; binary-mode files take bytes, and you have bytes. Meanwhile, I don't know any good tutorials. You can see if any are mentioned in the tagwiki (click on the python tag under your question), or maybe the Community section at python.org. – abarnert Apr 26 '18 at 00:55

0 Answers0