0

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?

marcusshep
  • 1,916
  • 2
  • 18
  • 31
hillz
  • 537
  • 6
  • 10
  • 18
  • Possible duplicate of [How to handle a broken pipe (SIGPIPE) in python?](http://stackoverflow.com/questions/180095/how-to-handle-a-broken-pipe-sigpipe-in-python) – quamrana Nov 17 '16 at 17:52

2 Answers2

0

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.

Community
  • 1
  • 1
Noah Christopher
  • 166
  • 1
  • 13
  • if it's bad practice why post it as a solution? – marcusshep Nov 17 '16 at 17:42
  • It will keep his program from throwing exceptions but in doing so he may end up eating other exceptions. There is a more elegant solution in the link I posted, but OP may not need that solution if he is trying to do something quick and dirty. However I wanted to make sure he was aware of both ways. – Noah Christopher Nov 17 '16 at 17:49
  • He/she's clearly aware. They use try/except several times throughout their script. – marcusshep Nov 17 '16 at 17:50
0

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.

E.g.

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
quantummind
  • 2,086
  • 1
  • 14
  • 20
  • How do I handle any kind of exception ? so it's not just one – hillz Nov 17 '16 at 21:24
  • Use multiple except lines. Simple example from python docs (check the example with multiple except lines): https://docs.python.org/2.7/tutorial/errors.html#handling-exceptions – quantummind Nov 17 '16 at 21:36