Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SIGPIPE With Python

DZone's Guide to

SIGPIPE With Python

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

Have you ever seen a socket.error: [Errno 32] Broken pipe message when running a Python Web server and wondered what that means?

The rule is that when a process tries to write to a socket that has already received an RST packet, the SIGPIPE signal is sent to that process which causes the Broken pipe socket.error exception.

Here are two scenarios that you can try that cause SIGPIPE signal to be fired.

1. Server may send an RST packet to a client to abort the socket connection but the client ignores the packet and continues to write to the socket.

To test that behavior install Cynic, run it

$ cynic
INFO     [2012-06-08 05:06:37,040] server: Starting 'HTTPHtmlResponse'   on port 2000
INFO     [2012-06-08 05:06:37,040] server: Starting 'HTTPJsonResponse'   on port 2001
INFO     [2012-06-08 05:06:37,040] server: Starting 'HTTPNoBodyResponse' on port 2002
INFO     [2012-06-08 05:06:37,040] server: Starting 'HTTPSlowResponse'   on port 2003
INFO     [2012-06-08 05:06:37,040] server: Starting 'RSTResponse'        on port 2020
INFO     [2012-06-08 05:06:37,040] server: Starting 'RandomDataResponse' on port 2021
INFO     [2012-06-08 05:06:37,040] server: Starting 'NoResponse'         on port 2022
INFO     [2012-06-08 05:06:37,041] server: Starting 'LogRecordHandler'   on port /tmp/_cynic.sock

 and then run the client1.py:

import socket
 
# connect to Cynic's RSTResponse service on port 2020
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 2020))
 
# first read gets an RST packet
try:
    s.recv(1024)
except socket.error as e:
    print e
    print
 
# write after getting the RST causes SIGPIPE signal
# to be sent to this process which causes a socket.error
# exception
s.send('hello')

 and see what happens:

$ python ./client1.py
[Errno 104] Connection reset by peer
 
Traceback (most recent call last):
  File "./client1.py", line 17, in
    s.send('hello')
socket.error: [Errno 32] Broken pipe

2. Server can send an RST to the client’s SYN request to indicate that there is no process wating for connections on the host at the specified port, but the client tries to write to the socket anyway.

To test it run the client2.py:

import socket
 
# connect to the port that nobody is listening on
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
# gets us an RST packet
try:
    s.connect(('', 30301))
except socket.error as e:
    print e
    print
 
# write after getting RST causes SIGPIPE signal
# to be sent to this process which causes an exception
s.send('hello')

Here is the output: 

$ python ./client2.py
[Errno 111] Connection refused
 
Traceback (most recent call last):
  File "./sigpipe2.py", line 15, in
    s.send('hello')
socket.error: [Errno 32] Broken pipe

 I hope that clarifies a SIGPIPE’s nature a little bit.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Will Soprano. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}