"TypeError: 'str' does not support the buffer interface" when writing to a socket
<pre><code>import socket

def Main():
   host = '127.0.0.1'
   port = 5000

s = socket.socket()
s.connect((host, port))

filename = input("Filename? -&gt; ")
if filename != 'q':
    s.send(filename)
    data = s.recv(1024)
    if data[:6] == 'EXISTS':
        filesize = long(data[6:])
        message = input("File Exists, " + str(filesize) +\
                        "Bytes, download? (Y/N) -&gt; ")
        if message == 'Y':
            s.send('OK')
            f = open('new_' + filename, 'wb')
            data = s.recv(1024)
            totalRecv = len(data)
            f.write(data)
            while totalRecv &lt; filesize:
                data = s.recv(1024)
                totalRecv += len(data)
                f.write(data)
                print("{0:.2f}".format((totalRecv/float(filesize))*100)+\
                      "% Done")
                print("Download complete!")
    else:
        print("File doedn't exist!")

s.close()

if __name__ == '__main__':
   Main()
</code></pre>

<p>The above python code is giving me this error: </p>

<pre class="lang-none prettyprint-override"><code>File "C:/Users/Mario/Networking/File Trasfering/fileClient.py", line 12, in Main

s.send(filename)
TypeError: 'str' does not support the buffer interface
</code></pre>

<p>Python 3 makes a clean distinction between text strings (in the unicode character set, but conceptually independent of any encoding) and sequences of bytes. To write text to a file or socket, it must be "encoded" using a suitable encoding. In your case, you could use </p>

<pre><code>s.send(bytes(filename, encoding="..."))
</code></pre>

<p>or the equivalent</p>

<pre><code>s.send(filename.encode(encoding="..."))
</code></pre>

<p>Literal strings can simply be written as byte strings, e.g.,</p>

<pre><code>s.send(b'OK')
</code></pre>

python sockets python-3.x networking