开发者

catching a broken socket in python

I'm having problems detecting a broken socket when a broken pipe exception occurs. See the below code for an example:

The Server:

import errno, select, socket, time, SocketServer

class MetaServer(object):
    def __init__(self):
        self.server = Server(None, Handler, bind_and_activate=False)
    def run(self, sock, addr):
        rfile = sock.makefile('rb', 1)
        self.server.process_request(sock, addr)
        while 1:
            r, _, _ = select.select([rfile], [], [], 1.0)
            if r:
                print 'Got %s' % rfile.readline()
            else:
                print 'nothing to read'

class Server(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    allow_reuse_address = True
    daemon_threads = True

class Handler(SocketServer.StreamRequestHandler):
    def handle(self):
        print 'connected!'
        try:
            while 1:
                self.wfile.write('testing...')
                time.sleep(1)
        except socket.error as e:
            if e.errno == errno.EPIPE:
                print 'Broken pipe!'
        self.finish()
        self.request.close()

if __name__ == '__main__':
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('127.0.0.1', 8081))
    s.listen(5)
    ms = MetaServer()
    while 1:
        client, address = s.accept()
        ms.run(client, address)

The Client:

import select, socket

s = socket.socket(socket开发者_StackOverflow.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8081))
while 1:
    r, _, _ = select.select([s], [], [], 1.0)
    if not r:
        continue
    msg = s.recv(1024)
    print 'Got %s' % (msg,)

Now, if I run the server and client, all is well, and I get a "nothing is read" message every second. As soon as I CTRL-C out of the client, the server goes crazy and starts to "read" from what should be a busted socket, dumping a lot of "Got " messages.

Is there some way to detect this broken socket in the MetaServer.run() function to avoid the above said behavior?


Yes, that's something which is not really in the documentation but old Un*x behavior: You need to abort when you get an empty string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜