Problems with asynchronous python working with AF_UNIX sockets

I have some problems using asyncore with AF_UNIX sockets. This code

import asyncore, socket, os
class testselect(asyncore.dispatcher):

    path = '/tmp/mysocket'

    def __init__(self):

        asyncore.dispatcher.__init__(self)

        self.create_socket(socket.AF_UNIX, socket.SOCK_DGRAM)
        self.bind(self.path)
        self.buffer = 'buffer'

    def handle_connect(self):

        print 'handle_connect'
        pass

    def handle_close(self):
        print 'handle_close'
        if os.path.exists(self.path)       
             os.remove(self.path)
        self.close()

    def handle_read(self):
        print 'handle_read'
        print self.recv(8192)

    def writable(self):
        print 'writable'
        return (len(self.buffer) > 0)

    def handle_write(self):
        print 'handle_write'
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]


    client = testselect()
    asyncore.loop()

If I execute the code

 $ python select_prova.py
 writable
 handle_connect
 handle_write
 handle_close
 $  

It shuts down immediately and does not wait for reading and writing. If I change the code to force the writeable () method to return, it always Falseworks correctly for input, and I can communicate with a similar community

 $ socat readline UNIX:/tmp/mysocket

But read-only (writing logically does not work because writeable () returns False). Is there an error in my code, or am I unable to control the AF_UNIX sockets using asyncore / select ()?

+3
source share
2 answers

. , . , testselect , .

asyncore examples, , . TimeChannel , - socket.AF_INET socket.AF_UNIX , UNIX.


socket.SOCK_DGRAM, UDP INET. Unix - IPC. socket.SOCK_STREAM, self.listen([backlog]), handle_accept() ..

SOCK_DGRAM AF_UNIX, , , , writable, handle_write, , 'buffer'.

, , handle_connect handle_read:

    def __init__(self):
        ...
        self.buffer = ''

    def handle_connect(self):
        self.buffer = 'buffer'

, , , socat.


, , :

import asyncore, socket, os

class testselect(asyncore.dispatcher):

    path = '/tmp/mysocket'

    def __init__(self):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(self.path)
        self.listen(5)

    def handle_accept(self):
        client = self.accept()
        if client is None:
            pass
        else:
            handler = testhandler(*client)

class testhandler(asyncore.dispatcher_with_send):

    def __init__(self, sock, addr):
        asyncore.dispatcher_with_send.__init__(self, sock)
        self.addr = addr
        self.buffer = 'greetings'

    def handle_read(self):
        print self.recv(8192)

    def writable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        self.send(self.buffer)
        self.buffer = ''

    def handle_close(self):
        self.close()

server = testselect()
try:
    asyncore.loop()
finally:
    if os.path.exists(testselect.path):
        os.unlink(testselect.path)
+5

, SOCK_DGRAM. , , SOCK_DGRAM asyncore ( recvfrom sendto). , , , SOCK_DGRAM UNIX.

SOCK_DGRAM , . write, , .

, . SOCK_STREAM.

+1

All Articles