简体   繁体   中英

How to prevent python script from exiting when it hits [Errno 32] Broken pipe

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?

You could use a blank except policy, but that is a bad practice typically. It would look like `

try:
     self.channel[self.s].send(netdata)
except Exception, e:
     print e
except:
     code for blanket exception

` Check out How to handle a broken pipe (SIGPIPE) in python? it seems to be a very similar to your problem.

You did not provide a working example, so the answer can be only little bit theoretical. Try to make exception handling to every point where exception can happen.

Eg

while 1:
    # Do stuff
    self.on_outbounddata()

No exception handling here. Only partly inside of the function.

Also here you do not handle errors:

    try:
        server.main_loop()
    except KeyboardInterrupt:
        print "Ctrl C - Stopping server"

Here you only handle one type of exception:

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM