Note that there are some explanatory texts on larger screens.

plurals
  1. POSend multiple data (text and images) to a server through a socket
    primarykey
    data
    text
    <p>I've made a test server in python, which receives a connection through a socket and save png files. But, I want to pass some other data to the server like the file's name, user who send it, etc. But I can't do this, because to receive the data you must inform the number of bytes you are reading in buffer.</p> <p>I've researched a lot, some examples says to use struct and pack all data, but the size of one image to another is different and I can't simply make a struct format, because it will be different for each file I receive.</p> <p>This is what I have working so far:</p> <h3>SERVER</h3> <pre><code>import socket import os import sys FilePath = os.path.realpath(os.path.dirname(sys.argv[0])) s = socket.socket() s.bind(("localhost",8000)) s.listen(5) #Tipo, 5 conexoes no maximo {ao mesmo tempo} i=0 name = 'file_' while (True): sc, address = s.accept() name = 'file_%s' % i f = open(os.path.join(FilePath,'server_received/%s.png'% name) ,'wb') #open as binary data i=i+1 # receives and writes the file l = sc.recv(1024) while (l): f.write(l) l = sc.recv(1024) f.close() sc.close() s.close() </code></pre> <h3>CLIENT</h3> <pre><code>class SendToServer(Thread): def __init__(self, queue, *args, **kwargs): Thread.__init__(self) self.queue = queue self.args = args self.kwargs = kwargs def run(self): try: while not self.queue.empty(): s = socket.socket() s.connect((HOST,PORT)) file_path = self.queue.get() file = open(file_path,'rb') s.send(file_path) l = file.read(1024) while l: s.send(l) l = file.read(1024) self.queue.task_done() s.close() print u"Enviado" except: print u"Sem conexao" #This i Use when I call the Thread: sync= SendToServer(queue) sync.run() </code></pre> <p>This code above is working well, but how can I send more data than just the file? (binary data)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload