DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > SIGPIPE With Python

SIGPIPE With Python

Will Soprano user avatar by
Will Soprano
·
Jun. 14, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
8.39K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Python (language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Which JVM Version Is the Fastest?
  • Debugging Deadlocks and Race Conditions

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo