title: Sending image over sockets (ONLY) in Python, image can not be open
body: <p>I want to upload the image from client to server using ONLY sockets in Python. I designed a very simple protocol flow of how I would like to do this:</p>
  
  <pre><code>CLIENT                                       SERVER
                 SIZE 512 (send image size)
           ----------------------------------&gt;
                        GOT SIZE   
           &lt;---------------------------------- 
                   send image itself
           ----------------------------------&gt;
                        GOT IMAGE
           &lt;----------------------------------
                        BYE BYE 
           ----------------------------------&gt;
                  server closes socket
  </code></pre>
  
  <p>Here's my client code:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import random
  import socket, select
  from time import gmtime, strftime
  from random import randint
  
  image = "tux.png"
  
  HOST = '127.0.0.1'
  PORT = 6666
  
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_address = (HOST, PORT)
  sock.connect(server_address)
  
  try:
  
      # open image
      myfile = open(image, 'rb')
      bytes = myfile.read()
      size = len(bytes)
  
      # send image size to server
      sock.sendall("SIZE %s" % size)
      answer = sock.recv(4096)
  
      print 'answer = %s' % answer
  
      # send image to server
      if answer == 'GOT SIZE':
          sock.sendall(bytes)
  
          # check what server send
          answer = sock.recv(4096)
          print 'answer = %s' % answer
  
          if answer == 'GOT IMAGE' :
              sock.sendall("BYE BYE ")
              print 'Image successfully send to server'
  
      myfile.close()
  
  finally:
      sock.close()
  </code></pre>
  
  <p>And my server, which receives images from clients:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import random
  import socket, select
  from time import gmtime, strftime
  from random import randint
  
  imgcounter = 1
  basename = "image%s.png"
  
  HOST = '127.0.0.1'
  PORT = 6666
  
  connected_clients_sockets = []
  
  server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  server_socket.bind((HOST, PORT))
  server_socket.listen(10)
  
  connected_clients_sockets.append(server_socket)
  
  while True:
  
      read_sockets, write_sockets, error_sockets = select.select(connected_clients_sockets, [], [])
  
      for sock in read_sockets:
  
          if sock == server_socket:
  
              sockfd, client_address = server_socket.accept()
              connected_clients_sockets.append(sockfd)
  
          else:
              try:
  
                  data = sock.recv(4096)
                  txt = str(data)
  
                  if txt.startswith('SIZE'):
                      tmp = txt.split()
                      size = int(tmp[1])
  
                      print 'got size'
  
                      sock.send("GOT SIZE")
  
                  elif txt.startswith('BYE'):
                      sock.shutdown()
  
                  elif data:
  
                      myfile = open(basename % imgcounter, 'wb')
  
                      data = sock.recv(40960000)
                      if not data:
                          myfile.close()
                          break
                      myfile.write(data)
                      myfile.close()
  
                      sock.send("GOT IMAGE")
                      sock.shutdown()
              except:
                  sock.close()
                  connected_clients_sockets.remove(sock)
                  continue
          imgcounter += 1
  server_socket.close()
  </code></pre>
  
  <p>For the example image:</p>
  
  <p><a href="https://i.stack.imgur.com/CNyA4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CNyA4.png" alt="enter image description here"></a></p>
  
  <p>The client prints out messages, that suggest, that it send the image to server successfully:</p>
  
  <pre><code>answer = GOT SIZE
  answer = GOT IMAGE
  Image successfully send to server
  </code></pre>
  
  <p>The image is created at server's side, but I can not open it. And yes, I tried different images. With no success. I can't open them at server's side, even though they are available there. </p>
  
  <p>Thanks to @BorrajaX, I managed to make it work! Thanks! :)</p>
  
  <pre><code>#!/usr/bin/env python
  
  import random
  import socket, select
  from time import gmtime, strftime
  from random import randint
  
  imgcounter = 1
  basename = "image%s.png"
  
  HOST = '127.0.0.1'
  PORT = 6666
  
  connected_clients_sockets = []
  
  server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  server_socket.bind((HOST, PORT))
  server_socket.listen(10)
  
  connected_clients_sockets.append(server_socket)
  
  while True:
  
      read_sockets, write_sockets, error_sockets = select.select(connected_clients_sockets, [], [])
  
      for sock in read_sockets:
  
          if sock == server_socket:
  
              sockfd, client_address = server_socket.accept()
              connected_clients_sockets.append(sockfd)
  
          else:
              try:
  
                  data = sock.recv(4096)
                  txt = str(data)
  
                  if data:
  
                      if data.startswith('SIZE'):
                          tmp = txt.split()
                          size = int(tmp[1])
  
                          print 'got size'
  
                          sock.sendall("GOT SIZE")
  
                      elif data.startswith('BYE'):
                          sock.shutdown()
  
                      else :
  
                          myfile = open(basename % imgcounter, 'wb')
                          myfile.write(data)
  
                          data = sock.recv(40960000)
                          if not data:
                              myfile.close()
                              break
                          myfile.write(data)
                          myfile.close()
  
                          sock.sendall("GOT IMAGE")
                          sock.shutdown()
              except:
                  sock.close()
                  connected_clients_sockets.remove(sock)
                  continue
          imgcounter += 1
  server_socket.close()
  </code></pre>