0

I was wondering how to handle forms that send files using a socket server in python.

This is what my server code currently looks like:

import socket               # Import socket module
import time

s = socket.socket()         # Create a socket object
port = 12345                # Reserve a port for your service.
s.bind(('', port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   data = c.recv(10240)
   with open('filename{}.txt'.format(addr), 'wb') as f:
      f.write(data)
   print(time.ctime(), ': Got connection from', c, addr)
   c.close()  

I am using a form whose action attribute points to localhost:12345. I am sending text data as well as attaching an image. However, when I look at the txt file, I only see the headers. How do I get the image from this?

Or am I going about this in completely the wrong direction?

So this is my edited code:

while True:
   c, addr = s.accept() # Establish connection with client.
   print(time.ctime(), ': Got connection from', c, addr)
   data = []
   bytes_recd = 0
   while 1:
      chunk = c.recv(10240)
      if chunk == b"" or len(chunk):
         break
      data.append(chunk)
      bytes_recd = bytes_recd + len(chunk)
      print(time.ctime(), bytes_recd)
   data = b"".join(data)
   print('reached here')

It stops printing the time, bytes_recd after everything has been received, but it never breaks the loop. Is there any reason why it does that?

Asish M.
  • 2,588
  • 1
  • 16
  • 31
  • This is most likely the completely wrong direction (hard to say without more information, but since you're mentioning forms and action attributes, it seems likely that what you want is a POST with the the correct encoding). The code also seems incorrect in many of its details (e.g. you're not guaranteed to get everything in the first recv call..). – thebjorn Jul 11 '16 at 20:21
  • It would be much easier to bring up a small http server that will already take care of all that. Also, you need to continue reading from the client socket, no one can be sure if the data will arrive all at once or by chunks. (`recv` does not wait until the buffer is full.) – Dean Fenster Jul 11 '16 at 20:23
  • So I'd loop over the receiving of data and catch any errors? @DeanFenster – Asish M. Jul 11 '16 at 20:38
  • @thebjorn I tried with POST requests and the txt file I ended up with, it only had the headers and no data. What would be the correct encoding? I'll try using a loop and finding an implementation that waits for all the data to be received. – Asish M. Jul 11 '16 at 20:38
  • Correct encoding would be `multipart/form-data` (see http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean and https://www.ietf.org/rfc/rfc2388.txt). While there is no magic involved in doing this "by-hand", it saves a lot of tedium if you select a framework that will do this for you. – thebjorn Jul 11 '16 at 20:43
  • I do have `enctype = "multipart/form-data"` in the form. Could you link to any sources for an implementation that would allow me to receive the data in chunks? – Asish M. Jul 11 '16 at 20:50
  • If you're asking about implementation details for socket code, then that is a large area you need to cover. Definitely read https://docs.python.org/2/howto/sockets.html, and google for "socket programming in python" for tutorials. It really is too big a topic to fit in a comment (or even an answer). – thebjorn Jul 11 '16 at 20:55
  • Made an edit in the question. @thebjorn Could you answer why it does that? – Asish M. Jul 11 '16 at 21:13

0 Answers0