blob: b5ffdbab9be8c694b96306b69825cd432faa86ce [file] [log] [blame]
Victor Stinner24f8ebf2014-01-23 11:05:01 +01001.. currentmodule:: asyncio
2
Victor Stinner4b4f9eb2014-01-24 17:33:20 +01003.. _streams:
4
Victor Stinner1374bd42014-01-24 15:34:19 +01005++++++++++++++++++++++++
6Streams (high-level API)
7++++++++++++++++++++++++
Victor Stinner24f8ebf2014-01-23 11:05:01 +01008
9Stream functions
10================
11
12.. function:: open_connection(host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds)
13
14 A wrapper for :meth:`~BaseEventLoop.create_connection()` returning a (reader,
15 writer) pair.
16
17 The reader returned is a :class:`StreamReader` instance; the writer is
18 a :class:`StreamWriter` instance.
19
20 The arguments are all the usual arguments to
21 :meth:`BaseEventLoop.create_connection` except *protocol_factory*; most
22 common are positional host and port, with various optional keyword arguments
23 following.
24
25 Additional optional keyword arguments are *loop* (to set the event loop
26 instance to use) and *limit* (to set the buffer limit passed to the
27 :class:`StreamReader`).
28
29 (If you want to customize the :class:`StreamReader` and/or
30 :class:`StreamReaderProtocol` classes, just copy the code -- there's really
31 nothing special here except some convenience.)
32
33 This function returns a :ref:`coroutine object <coroutine>`.
34
35.. function:: start_server(client_connected_cb, host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds)
36
37 Start a socket server, call back for each client connected.
38
39 The first parameter, *client_connected_cb*, takes two parameters:
40 *client_reader*, *client_writer*. *client_reader* is a
41 :class:`StreamReader` object, while *client_writer* is a
42 :class:`StreamWriter` object. This parameter can either be a plain callback
43 function or a :ref:`coroutine function <coroutine>`; if it is a coroutine
44 function, it will be automatically converted into a :class:`Task`.
45
46 The rest of the arguments are all the usual arguments to
47 :meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most
48 common are positional host and port, with various optional keyword arguments
49 following. The return value is the same as
50 :meth:`~BaseEventLoop.create_server()`.
51
52 Additional optional keyword arguments are *loop* (to set the event loop
53 instance to use) and *limit* (to set the buffer limit passed to the
54 :class:`StreamReader`).
55
56 The return value is the same as :meth:`~BaseEventLoop.create_server()`, i.e.
57 a :class:`AbstractServer` object which can be used to stop the service.
58
59 This function returns a :ref:`coroutine object <coroutine>`.
60
61
62StreamReader
63============
64
65.. class:: StreamReader(limit=_DEFAULT_LIMIT, loop=None)
66
67 .. method:: exception()
68
69 Get the exception.
70
71 .. method:: feed_eof()
72
73 XXX
74
75 .. method:: feed_data(data)
76
77 XXX
78
79 .. method:: set_exception(exc)
80
81 Set the exception.
82
83 .. method:: set_transport(transport)
84
85 Set the transport.
86
87 .. method:: read(n=-1)
88
89 XXX
90
91 This method returns a :ref:`coroutine object <coroutine>`.
92
93 .. method:: readline()
94
95 XXX
96
97 This method returns a :ref:`coroutine object <coroutine>`.
98
99 .. method:: readexactly(n)
100
Victor Stinnerb7f19ff2014-01-27 11:58:49 +0100101 Read exactly *n* bytes. Raise an :exc:`IncompleteReadError` if the end of
102 the stream is reached before *n* can be read, the
103 :attr:`IncompleteReadError.partial` attribute of the exception contains
104 the partial read bytes.
Victor Stinner24f8ebf2014-01-23 11:05:01 +0100105
106 This method returns a :ref:`coroutine object <coroutine>`.
107
108
109StreamWriter
110============
111
112.. class:: StreamWriter(transport, protocol, reader, loop)
113
114 Wraps a Transport.
115
116 This exposes :meth:`write`, :meth:`writelines`, :meth:`can_write_eof()`,
117 :meth:`write_eof`, :meth:`get_extra_info` and :meth:`close`. It adds
118 :meth:`drain` which returns an optional :class:`Future` on which you can
119 wait for flow control. It also adds a transport attribute which references
120 the :class:`Transport` directly.
121
122 .. attribute:: transport
123
124 Transport.
125
126 .. method:: close()
127
128 Close the transport: see :meth:`BaseTransport.close`.
129
130 .. method:: drain()
131
Victor Stinner62f8ecc2014-01-24 18:47:26 +0100132 Wait until the write buffer of the underlying transport is flushed.
Victor Stinner24f8ebf2014-01-23 11:05:01 +0100133
Victor Stinner62f8ecc2014-01-24 18:47:26 +0100134 This method has an unusual return value. The intended use is to write::
Victor Stinner24f8ebf2014-01-23 11:05:01 +0100135
136 w.write(data)
137 yield from w.drain()
138
139 When there's nothing to wait for, :meth:`drain()` returns ``()``, and the
140 yield-from continues immediately. When the transport buffer is full (the
141 protocol is paused), :meth:`drain` creates and returns a
142 :class:`Future` and the yield-from will block until
143 that Future is completed, which will happen when the buffer is
144 (partially) drained and the protocol is resumed.
145
146 .. method:: get_extra_info(name, default=None)
147
148 Return optional transport information: see
149 :meth:`BaseTransport.get_extra_info`.
150
151 .. method:: write(data)
152
153 Write some *data* bytes to the transport: see
154 :meth:`WriteTransport.write`.
155
156 .. method:: writelines(data)
157
158 Write a list (or any iterable) of data bytes to the transport:
159 see :meth:`WriteTransport.writelines`.
160
161 .. method:: can_write_eof()
162
163 Return :const:`True` if the transport supports :meth:`write_eof`,
164 :const:`False` if not. See :meth:`WriteTransport.can_write_eof`.
165
166 .. method:: write_eof()
167
168 Close the write end of the transport after flushing buffered data:
169 see :meth:`WriteTransport.write_eof`.
170
171
172StreamReaderProtocol
173====================
174
175.. class:: StreamReaderProtocol(stream_reader, client_connected_cb=None, loop=None)
176
177 Trivial helper class to adapt between :class:`Protocol` and
178 :class:`StreamReader`. Sublclass of :class:`Protocol`.
179
180 *stream_reader* is a :class:`StreamReader` instance, *client_connected_cb*
181 is an optional function called with (stream_reader, stream_writer) when a
182 connection is made, *loop* is the event loop instance to use.
183
184 (This is a helper class instead of making :class:`StreamReader` itself a
185 :class:`Protocol` subclass, because the :class:`StreamReader` has other
186 potential uses, and to prevent the user of the :class:`StreamReader` to
187 accidentally call inappropriate methods of the protocol.)
188
189 .. method:: connection_made(transport)
190
191 XXX
192
193 .. method:: connection_lost(exc)
194
195 XXX
196
197 .. method:: data_received(data)
198
199 XXX
200
201 .. method:: eof_received()
202
203 XXX
204
205 .. method:: pause_writing()
206
207 XXX
208
209 .. method:: resume_writing()
210
211 XXX
212
Victor Stinnerc520edc2014-01-23 11:25:48 +0100213
Victor Stinnerb7f19ff2014-01-27 11:58:49 +0100214IncompleteReadError
215===================
216
217.. exception:: IncompleteReadError
218
Victor Stinner32970b82014-01-27 12:18:49 +0100219 Incomplete read error, subclass of :exc:`EOFError`.
Victor Stinnerb7f19ff2014-01-27 11:58:49 +0100220
221 .. attribute:: expected
222
223 Total number of expected bytes (:class:`int`).
224
225 .. attribute:: partial
226
227 Read bytes string before the end of stream was reached (:class:`bytes`).
228
229
Victor Stinnerc520edc2014-01-23 11:25:48 +0100230Example
231=======
232
233Simple example querying HTTP headers of the URL passed on the command line::
234
235 import asyncio
236 import urllib.parse
237 import sys
238
239 @asyncio.coroutine
240 def print_http_headers(url):
241 url = urllib.parse.urlsplit(url)
242 reader, writer = yield from asyncio.open_connection(url.hostname, 80)
243 query = ('HEAD {url.path} HTTP/1.0\r\n'
244 'Host: {url.hostname}\r\n'
245 '\r\n').format(url=url)
246 writer.write(query.encode('latin-1'))
247 while True:
248 line = yield from reader.readline()
249 if not line:
250 break
251 line = line.decode('latin1').rstrip()
252 if line:
253 print('HTTP header> %s' % line)
254
255 url = sys.argv[1]
256 loop = asyncio.get_event_loop()
257 task = asyncio.async(print_http_headers(url))
258 loop.run_until_complete(task)
Victor Stinnerf40c6632014-01-28 23:32:40 +0100259 loop.close()
Victor Stinnerc520edc2014-01-23 11:25:48 +0100260
261Usage::
262
263 python example.py http://example.com/path/page.html
264