. , . , 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)