How to fix broken pipe in Chat server using socket in Python after first request?
<p>I am playing with socket and tried to create simple chat server with only one client connection. Code and output as follows.</p>

<p>echo_server.py</p>

<pre><code>import socket

host = ''
port = 4538
backlog = 5
size = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
print "Starting Server"

while 1:
    client, address = s.accept()
    try:
        data = client.recv(size)
        if data is not None:
        if data is 'q':
        print "I received request to close the connection"
        client.send('q')
                continue
        print "I got this from client {}".format(data)
            client.send(data)
            continue
            if data == 0:
                client.close()
    finally:
        client.close()
</code></pre>

<p>echo_client.py</p>

<pre><code>import socket

host = ''
port = 4538

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

try:
    while 1:
        message = filename = raw_input('Enter a your message: ')
    s.send(message)
    data = s.recv(1024)
        if data is 'q':
        print "You requested to close the connection"
            break
    print "Received from socket {}".format(data)
finally:
    s.close()
</code></pre>

<p>Now, I had tried with sendall() too but it doesn't work. Following is the output on both sides</p>

<p>client:</p>

<pre><code>Enter a your message: hello
Received from socket hello
Enter a your message: world
Received from socket 
Enter a your message: hi
Traceback (most recent call last):
  File "echo_client.py", line 12, in &lt;module&gt;
    s.send(message)
socket.error: [Errno 32] Broken pipe
</code></pre>

<p>And on server</p>

<pre><code>Starting Server
I got this from client hello
</code></pre>

<p>As you can see, the server doesn't get the second message(world). And replies with nothing and when I send third request to server with hi, client terminates with <strong>Broken Pipe</strong> 
How do I go about fixing it?</p>

<p>EDIT 1:</p>

<p>I changed the code now it's following. The s.accept() gets stuck in second request. Following is the code.
echo_server.py</p>

<pre><code>import socket

host = ''
port = 4538
backlog = 5
size = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
print "Starting Server"

try:
    while 1:
    print "BEFORE REQUEST"
        client, address = s.accept()
    print "AFTER REQUEST"
        data = client.recv(size)
        if data:
        if data is 'q':
                print "I received request to close the connection"
            client.send('q')
        print "I got this from client {}".format(data)
            client.send(data)
        else:
        print "CLOSING IN ELSE"
            client.close()
except:
    print "CLOSING IN except"
    client.close()
</code></pre>

<p>Following is the output.</p>

<pre><code>BEFORE REQUEST
AFTER REQUEST
I got this from client hello
BEFORE REQUEST
</code></pre>

<p>As you can see for the second time accept() never returns. How to make it working?</p>

<p><code>recv</code> returns empty string when the client is closes the connection, not <code>None</code> or <code>0</code>.  Since empty string is a False condition, simply use <code>if data:</code> or <code>if not data:</code>.</p>

<p>And, as @JonClements pointed out, use an <code>except</code> instead of a <code>finally</code> in the server, or put the <code>while</code> inside the <code>try</code> and <code>if not data: break</code> to exit the <code>while</code> and execute the <code>finally</code>. </p>

python sockets broken-pipe