我有一個python腳本和我使用的是while loop
到永遠循環下去,我的劇本是這樣的:如何防止Python腳本從離開時,命中[錯誤32]破管
#!/usr/bin/python
import socket,select,time,base64,os,sys,re,datetime
def on_outbounddata(self):
print "ON_OUTBOUNDDATA"
netdata = self.netdata
if netdata.find('HTTP/1.') ==0:
ms = re.search(r"\^s(\d+)\^", payload)
if ms:
print "Sleeping for " + ms.group(1) + "ms"
dec = int(ms.group(1))/float(1000)
time.sleep(dec)
print self.request[self.s]
try:
self.channel_[self.s].send(self.request[self.s])
self.request[self.s]=''
except ValueError:
print "self.s is not in the list (on_outbounddata)"
pass
netdata='HTTP/1.1 200 Connection established\r\n\r\n'
try:
self.channel[self.s].send(netdata)
except Exception, e:
print e
def main_loop(self):
while 1:
# Do stuff
self.on_outbounddata()
# Do stuff
if __name__ == '__main__':
server = TheServer('0.0.0.0', listen)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
問題是,即使我有一個while循環,有時腳本將自行退出,當它遇到以下異常:
Traceback (most recent call last):
File "/usr/bin/socks", line 206, in <module>
server.main_loop()
File "/usr/bin/socks", line 121, in main_loop
self.on_outbounddata()
File "/usr/bin/socks", line 190, in on_outbounddata
self.channel_[self.s].send(self.request[self.s])
socket.error: [Errno 32] Broken pipe
我想我的劇本繼續即使excounters此異常socket.error: [Errno 32] Broken pipe
。我怎樣才能做到這一點?
的可能的複製[如何處理在Python破裂的管道(SIGPIPE)? ](http://stackoverflow.com/questions/180095/how-to-handle-a-broken-pipe-sigpipe-in-python) – quamrana