[Twisted-Python] Named Pipes

Moshe Zadka m at moshez.org
Fri May 16 11:36:38 MDT 2003


On Sat, 17 May 2003, Wari Wahab <wari at home.wari.org> wrote:

> Hi, is there a way to get twisted to listen to a named pipe like it's a 
> TCP connection or something? How would I go about it?

It's fairly easy, and probably should be done in Twisted at some point.

from twisted.internet import abstract, fdesc

class SelectableFile(abstract.FileDescriptor):

    def __init__(self, fp, protocol):
        self.fp = fp
        fdesc.setNonBlocking(fp)
        self.protocol = protocol
        self.protocol.makeConnection(self)
        self.fileno = self.fp.fileno

    def doRead(self):
        buf = self.fp.read(4096)
        if buf:
            self.protocol.dataReceived(buf)
        else:
            self.protocol.connectionLost()

    def write(self, data):
        pass # what can we do with the data?

    def loseConnection(self):
        self.fp.close()


reactor.addReader(SelectableFile(open("myNamedPipe"), protocolInstance))
[this is completely untested]

-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list