Python的SIGPIPE訊號
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
02.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPHtmlResponse' on port 2000
03.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPJsonResponse' on port 2001
04.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPNoBodyResponse' on port 2002
05.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPSlowResponse' on port 2003
06.INFO [2012-06-08 05:06:37,040] server: Starting 'RSTResponse' on port 2020
07.INFO [2012-06-08 05:06:37,040] server: Starting 'RandomDataResponse' on port 2021
08.INFO [2012-06-08 05:06:37,040] server: Starting 'NoResponse' on port 2022
09.INFO [2012-06-08 05:06:37,041] server: Starting 'LogRecordHandler' on port /tmp/_cynic.sockand then run the client1.py:
02.
03.# connect to Cynic's RSTResponse service on port 2020
04.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
05.s.connect(('', 2020))
06.
07.# first read gets an RST packet
08.try:
09.s.recv(1024)
10.except socket.error as e:
11.print e
12.print
13.
14.# write after getting the RST causes SIGPIPE signal
15.# to be sent to this process which causes a socket.error
16.# exception
17.s.send('hello')
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
CODE:
01.$ cynic02.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPHtmlResponse' on port 2000
03.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPJsonResponse' on port 2001
04.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPNoBodyResponse' on port 2002
05.INFO [2012-06-08 05:06:37,040] server: Starting 'HTTPSlowResponse' on port 2003
06.INFO [2012-06-08 05:06:37,040] server: Starting 'RSTResponse' on port 2020
07.INFO [2012-06-08 05:06:37,040] server: Starting 'RandomDataResponse' on port 2021
08.INFO [2012-06-08 05:06:37,040] server: Starting 'NoResponse' on port 2022
09.INFO [2012-06-08 05:06:37,041] server: Starting 'LogRecordHandler' on port /tmp/_cynic.sockand then run the client1.py:
CODE:
01.import socket02.
03.# connect to Cynic's RSTResponse service on port 2020
04.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
05.s.connect(('', 2020))
06.
07.# first read gets an RST packet
08.try:
09.s.recv(1024)
10.except socket.error as e:
11.print e
12.print
13.
14.# write after getting the RST causes SIGPIPE signal
15.# to be sent to this process which causes a socket.error
16.# exception
17.s.send('hello')
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733074/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 訊號量Python
- python中的訊號通訊 blinkerPython
- 初步理解Python程式的訊號通訊Python
- 處理python中的訊號Python
- Python訊號之分享Python
- python 之訊號SignalPython
- Python捕捉系統訊號Python
- Python執行緒專題5:訊號量與有邊界的訊號量Python執行緒
- Python 音訊訊號處理庫 librosaPython音訊ROS
- python學習之訊號量Python
- Bold訊號與fNIRS訊號的區別
- signal(SIGPIPE, SIG_IGN)
- 細說 ReactiveCocoa 的冷訊號與熱訊號(三):怎麼處理冷訊號與熱訊號React
- 程序間的通訊(訊號通訊)
- Python語音訊號處理的一些kitPython音訊
- linux中的兩個非常重要的訊號:SIGALRM訊號和SIGCHID訊號LinuxGC
- django的訊號量Django
- 細說 ReactiveCocoa 的冷訊號與熱訊號(1)React
- 音訊訊號表徵音訊
- 程序通訊-訊號
- 程序通訊--訊號
- SIGALRM訊號和SIGCHID訊號GC
- Python中使用共享變數+訊號量實現程序間的實時通訊Python變數
- 訊號課組(一) 訊號與系統 Part 0 MATLAB在訊號與系統中的使用Matlab
- django 訊號Django
- 程序訊號
- Linux訊號機制與訊號處理Linux
- 程式間通訊——POSIX 有名訊號量與無名訊號量
- Linux 未決訊號集與訊號遮蔽字Linux
- Qt 5 中的訊號槽QT
- system -v 訊號量的使用
- 分析外星訊號
- SIGCHLD訊號GC
- POSIX 訊號量
- 音訊訊號處理入門音訊
- Qt 訊號槽如何傳遞引數(或帶引數的訊號槽)QT
- 3分鐘看懂Python後端必須知道的Django的訊號機制!Python後端Django
- 訊號、系統與訊號處理邊角雜談