title: python socket OSError: [Errno 107] Transport endpoint is not connected
body: <p>So I've been making a socket filetransfer script with a client and a server. The server has files that the client can request here is the code.</p>
  
  <pre><code>#server.py
  import socket
  import os
  host = '127.0.0.1'
  port = 5000
  #defining the stuff we want to use
  #all the files you can download are stored here
  def main():
      file_folder = '/home/lario/Desktop/pyprojects/networking/filetransfer/files/'
  
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind((host, port))
      print('Server started on {}:{}'.format(host, str(port)))
      s.listen(5)
      sentace = 'We have these files for you to download: '
      while True:
          conn, addr = s.accept()
          print('[*] Got a connection from {}'.format(addr))
          loop(file_folder, sentace, conn, s, os.listdir(file_folder))
          print('[*] File transfer with {} has been succesfull')
      s.close()
  
  def loop(file_folder, sentace, conn, socket, existing_files):
      #sends which files we have to download for the client
      conn.send(sentace.encode('utf-8') + str(existing_files).encode('utf-8'))
      #gets response which one he wants to download
      data = socket.recv(4096)
      #gets path of the file
      file_place = file_folder + data.decode('utf-8')
      #checks if file exists
      if os.path.isfile(file_place):
          #asks if he wants to download it
          succes = '[+] The file exists, it has {} still wish to download?'.format(os.path.getsize(file_place)).encode('utf-8')
          conn.send(succes)
          data = socket.recv(4096).decode('utf-8')
          #if response is yes send him the file
          if data.lower() == 'y':
              with open(file_place, 'rb') as f:
                  file_data = f.read(4096)
                  conn.send(file_data)
          else:
              pass
  
  
      else:
          succes = '[-] The file doesn\'t exist. Try again'
          conn.send(succes.encode('utf-8'))
          loop(file_folder, sentace, conn, socket, existing_file)
  
  
  #client.py
  import socket
  def main():
      ip = '127.0.0.1'
      port = 5000
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((ip, port))
      print('[*] Connected to server')
      data = s.recv(1024)
      print(data.decode('utf-8'))
      file_down = input('--&gt;')
      s.send(file_down.encode('utf-8'))
      data = s.recv(4096)
      if data[1] == '+':
          data = s.recv(4096)
          print(data.decode('utf-8'))
          choice = input("Y/N").lower()
          if choice == 'y':
              s.send(choice.encode('utf-8'))
              prefix = input('What kind of prefix do you want for your files: ')
              with open(str(prefix + file_down), 'wb') as f:
                  data = s.recv(4096)
                  f.write(data)
          elif choice == 'n':
              s.send(choice.encode('utf-8'))
              print('Well, see you next time.')
          else:
              s.send(choice.encode('utf-8'))
              print('Not a valid input.')
  
      else:
          print('File doesn\'t exist.')
  
  
  main()
  </code></pre>
  
  <p>And this is the output.</p>
  
  <pre><code>#server
  Server started on 127.0.0.1:5000
  [*] Got a connection from ('127.0.0.1', 47170)
  Traceback (most recent call last):
    File "server.py", line 48, in &lt;module&gt;
      main()
    File "server.py", line 17, in main
      loop(file_folder, sentace, conn, s, os.listdir(file_folder))
    File "server.py", line 25, in loop
      data = socket.recv(4096).decode('utf-8')
  OSError: [Errno 107] Transport endpoint is not connected
  
  #client
  [*] Connected to server
  We have these files for you to download: ['syria.txt', 'eye.jpg', 'kim-jong.jpg']
  --&gt;
  </code></pre>
  
  <p>You will see I've got string in variables, I did it that way because you can't encode strings like this.</p>
  
  <pre><code>'this is a string'.encode('utf-8')
  </code></pre>
  
  <p>The client should choose a file with a prefix to it and it should write the file into anotherfile.</p>