TypeError: 'str' does not support the buffer interface in 3.4.1
Question: I've created a server in Python. The code is:

    
    
    from socket import *
    serverSocket = socket(AF_INET, SOCK_STREAM)
    localHost =  # my address
    port = 5369
    serverSocket.bind( (localHost, port) )
    serverSocket.listen(1)
    
    while True:
      print ("Ready to serve.....")
      connectionSocket, addr = serverSocket.accept()
    
    try:
        message = connectionSocket.recv(1024)
        filename = message.split()[1]
        f = open(filename[1:])
        outputdata = f.read()
    
      for i in range(0, len(outputdata) ):
          connectionSocket.send(outputdata[i])
    
      connectionSocket.close()
    
    except IOError:
        connectionSocket.send('404 Not Found')
    
    connectionSocket.close()
    serverSocket.close()
    

I executed the file. It gives an error:

    
    
    Traceback (most recent call last):
      File:"D;\.....\server.py", line 25, in <module>
      connectionSocket.send(outputdata[i])
    TypeError: 'str' does not support the buffer interface.
    

After scanning through similar queries on Stackoverflow,I found out that from
Python 3.0, the data needs to converted to bytes. If that's the case, then
where and how should I change/parse the data?


Answer: You opened your file in _text mode_ , which means that the contents are
decoded to unicode strings.

When writing to the socket you need to write `bytes` instead. You can easily
get bytes from a file by opening it in binary mode instead:

    
    
    with open(filename[1:], 'rb') as f:
        connectionSocket.send(f.read())
    

Note that I send the data to the socket _in one go_ ; no need to send the data
byte by byte here.

