I have a python script and I'm using a while loop
to loop forever, my script looks like this:
#!/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"
The problem is that even though I have a while loop, sometimes the script will exit on its own when it encounters the following exception:
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
I want my script to continue even though it excounters this exception socket.error: [Errno 32] Broken pipe
. How can I accomplish this?