asyncore and OOB data

billiejoex gnewsg at gmail.com
Wed Jul 11 19:48:07 CEST 2007


If it could be useful I attach the complete server application I used
for doing tests:


< code >

import asyncore, asynchat, socket, os

class Handler(asynchat.async_chat):
    def __init__(self, sock_obj):
        asynchat.async_chat.__init__(self, conn=sock_obj)
        self.remote_ip, self.remote_port = self.socket.getpeername()
        self.in_buffer = ""
        self.out_buffer = ""
        self.set_terminator("\r\n")

    def handle_expt(self):
        print "OOB data arrived"
        data = self.socket.recv(1024, socket.MSG_OOB)
        print tuple(data)

    def collect_incoming_data(self, data):
        self.in_buffer += data

    def found_terminator(self):
        print self.in_buffer
        self.in_buffer = ''

    def handle_error(self):
        raise


class Server(asyncore.dispatcher):

    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        if os.name == 'posix':
            self.set_reuse_addr()
        self.bind(('', 21))
        self.listen(5)

    def handle_accept(self):
        sock_obj, addr = self.accept()
        Handler(sock_obj)

    def handle_error(self):
        raise


Server()
asyncore.loop(timeout=1)




More information about the Python-list mailing list