title: TCP multi threading web server in python
body: <p>I have created a TCP multithreading webserver but it gives me the following 
  <strong>If the file is found</strong> 
  1. It says "int obj has no attribute encode"
  2. Later, it will show list index out of range </p>
  
  <p><strong>If the file is not found</strong> 
  1. It does not display the html error message on the web browser
  2. Later, it will show list index out of range </p>
  
  <p>My server code is </p>
  
  <pre><code>import socket
  import threading
  import os
  import sys
  
  #to convert bytes into string 
  def bytestoString(stringToRead):
          stringToRead = bytes.decode(stringToRead)
          type(stringToRead)
          return(stringToRead)
  
  #to conver string into bytes
  def stringToBytes(bytesToSend1):
          bytesToSend1= str.encode(bytesToSend1)
          type (bytes)
          return(bytesToSend1)
  #to retreive a file
  def retrFile(name,sock):
      message=sock.recv(1024)
      message_string = bytestoString(message)
      print(message_string)
      fileName=message_string.split()[1]
      fileName=fileName.split("/")[1]
      #stringLength=len(fileName)
      print(fileName)
  
      if os.path.isfile(fileName):
          print ('Ready to send the file................')
          with open(fileName,'rb') as fileRead:
              data= fileRead.read()
              print (data)
              exists_Bytes=stringToBytes('HTTP/1.1 200 OK\nContent-type=text/html')
              sock.send(exists_Bytes)
              for i in range(0 ,len(data)): 
                  sock.send((data[i]).encode())
              print('file sent succesfully')
              sock.close()
      else :
          httpResponse=stringToBytes('HTTP/1.1 404 not Found')
          sock.send(httpResponse)
          #errorDisplayPage=stringToBytes('&lt;html&gt;&lt;title&gt;404 Error&lt;/title&gt;&lt;body&gt;404 Error- Page cannot be found &lt;/body&gt;&lt;/html&gt;')
          sock.send(b'&lt;html&gt;&lt;title&gt;404 Error&lt;/title&gt;&lt;body&gt;404 Error- Page cannot be found &lt;/body&gt;&lt;/html&gt;')
          print ('error message displayed')  
          sock.close()
  
  
  def Main(serverPort):
      #creating a server socket type TCP
      serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      localHost=socket.gethostname()
      #binding the server to the client
      serverSocket.bind((localHost,serverPort))
  
      serverSocket.listen(5)
  
      print ('***Server is ready to recieve***')
  
      while True:
  
          connectionSocket, addr = serverSocket.accept()
          print ('got connection from:&lt;', addr,'&gt;')
          t=threading.Thread(target=retrFile,args=('retrThread',connectionSocket))
          t.start()
      connectionSocket.send('thank you for connecting')
      connectionSocket.close()
  
  
  if __name__ == '__main__':
      #getting server hostname and port number from the user
      serverPort=int(sys.argv[1])
      Main(serverPort)
  </code></pre>