title: Sending files between client - server through TCP socket in python?
body: <p>I'm trying to send and receive files through a TCP socket. When user types <code>put abc.txt</code>, abc.txt should be copied to the server.<br>
  When user types <code>get def.txt</code>, def.txt should be downloaded to the user computer. (Actually I have to implement 2 more methods - <code>ls</code> to list all files in the client directory and <code>lls</code> to list all files in the server, but I haven't done it yet.)</p>
  
  <p>Here's the code</p>
  
  <p><strong>Server</strong></p>
  
  <pre><code>import socket
  import sys
  HOST = 'localhost'                 
  PORT = 3820
  
  socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  socket.bind((HOST, PORT))
  
  socket.listen(1)
  while (1):
      conn, addr = socket.accept()
      print 'New client connected ..'
      reqCommand = conn.recv(1024)
      print 'Client&gt; %s' %(reqCommand)
      if (reqCommand == 'quit'):
          break
      #elif (reqCommand == lls):
          #list file in server directory
      else:
          string = reqCommand.split(' ', 1)   #in case of 'put' and 'get' method
          reqFile = string[1] 
  
          if (string[0] == 'put'):
              with open(reqFile, 'wb') as file_to_write:
                  while True:
                      data = conn.recv(1024)
                      if not data:
                          break
                      file_to_write.write(data)
                      file_to_write.close()
                      break
              print 'Receive Successful'
          elif (string[0] == 'get'):
              with open(reqFile, 'rb') as file_to_send:
                  for data in file_to_send:
                      conn.sendall(data)
              print 'Send Successful'
      conn.close()
  
  socket.close()
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>import socket
  import sys
  
  HOST = 'localhost'    #server name goes in here
  PORT = 3820
  
  def put(commandName):
      socket.send(commandName)
      string = commandName.split(' ', 1)
      inputFile = string[1]
      with open(inputFile, 'rb') as file_to_send:
          for data in file_to_send:
              socket.sendall(data)
      print 'PUT Successful'
      return 
  
  def get(commandName):
      socket.send(commandName)
      string = commandName.split(' ', 1) 
      inputFile = string[1]
      with open(inputFile, 'wb') as file_to_write:
          while True:
              data = socket.recv(1024)
              #print data
              if not data:
                  break
              print data
              file_to_write.write(data)
              file_to_write.close()
              break
      print 'GET Successful'
      return
  
  socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  socket.connect((HOST,PORT))
  
  msg = raw_input('Enter your name: ')
  while(1):
      print 'Instruction'
      print '"put [filename]" to send the file the server '
      print '"get [filename]" to download the file from the server '
      print '"ls" to list all files in this directory'
      print '"lls" to list all files in the server'
      print '"quit" to exit'
      sys.stdout.write ('%s&gt; ' %msg)
      inputCommand = sys.stdin.readline().strip()
      if (inputCommand == 'quit'):
          socket.send('quit')
          break
      # elif (inputCommand == 'ls')
      # elif (inputCommand == 'lls')
      else:
          string = inputCommand.split(' ', 1)
      if (string[0] == 'put'):
          put(inputCommand)
      elif (string[0] == 'get'):
          get(inputCommand)
  socket.close()
  </code></pre>
  
  <p>There are several problems that I couldn't fix.</p>
  
  <ol>
  <li>The program run correctly only on the first time (both 'put' and
  'get' method). After that, All commands from the client can't be
  sent to the server.</li>
  <li>The 'get' method doesn't work for an image/photo file.</li>
  </ol>