How to prevent python script from exiting when it hits [Errno 32] Broken pipe -
i have python script , i'm using while loop
loop forever, script looks 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 " + 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 not in 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: # stuff self.on_outbounddata() # stuff if __name__ == '__main__': server = theserver('0.0.0.0', listen) try: server.main_loop() except keyboardinterrupt: print "ctrl c - stopping server"
the problem though have while loop, script exit on own when encounters 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 script continue though excounters exception socket.error: [errno 32] broken pipe
. how can accomplish this?
you did not provide working example, answer can little bit theoretical. try make exception handling every point exception can happen.
e.g.
while 1: # stuff self.on_outbounddata()
no exception handling here. partly inside of function.
also here not handle errors:
try: server.main_loop() except keyboardinterrupt: print "ctrl c - stopping server"
here handle 1 type of exception:
try: self.channel_[self.s].send(self.request[self.s]) self.request[self.s]='' except valueerror: print "self.s not in list (on_outbounddata)" pass
Comments
Post a Comment