5 class Stream(object):
\r
7 a stream is a file-like object that is used to expose a consistent and uniform interface
\r
8 to the 'physical' file-like objects (like sockets and pipes), which have many quirks (sockets
\r
9 may recv() less than `count`, pipes are simplex and don't flush, etc.).
\r
10 a stream is always in blocking mode.
\r
14 raise NotImplementedError()
\r
17 raise NotImplementedError()
\r
19 def is_available(self):
\r
20 rlist, wlist, xlist = select.select([self], [], [], 0)
\r
24 select.select([self], [], [])
\r
26 def read(self, count):
\r
27 raise NotImplementedError()
\r
29 def write(self, data):
\r
30 raise NotImplementedError()
\r
33 class SocketStream(Stream):
\r
35 a stream that operates over a socket. note:
\r
36 * the socket is expected to be reliable (i.e., TCP)
\r
37 * the socket is expected to be in blocking mode
\r
39 def __init__(self, sock):
\r
43 host, port = self.sock.getpeername()
\r
44 return "<%s(%s:%d)>" % (self.__class__.__name__, host, port)
\r
46 def from_new_socket(cls, host, port, **kw):
\r
47 sock = socket.socket(**kw)
\r
48 sock.connect((host, port))
\r
50 from_new_socket = classmethod( from_new_socket )
\r
53 return self.sock.fileno()
\r
58 def read(self, count):
\r
61 buf = self.sock.recv(count)
\r
66 return "".join(data)
\r
68 def write(self, data):
\r
70 count = self.sock.send(data)
\r
74 class PipeStream(Stream):
\r
76 a stream that operates over two simplex pipes.
\r
77 note: the pipes are expected to be in blocking mode
\r
80 def __init__(self, incoming, outgoing):
\r
81 self.incoming = incoming
\r
82 self.outgoing = outgoing
\r
85 return self.incoming.fileno()
\r
88 self.incoming.close()
\r
89 self.outgoing.close()
\r
91 def read(self, count):
\r
94 buf = self.incoming.read(count)
\r
99 return "".join(data)
\r
101 def write(self, data):
\r
102 self.outgoing.write(data)
\r
103 self.outgoing.flush()
\r
107 if sys.platform == "win32":
\r
108 def is_available(self):
\r