File: tutorial.rst

package info (click to toggle)
python-dugong 3.7.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 524 kB
  • sloc: python: 2,714; makefile: 25; sh: 7
file content (296 lines) | stat: -rw-r--r-- 9,323 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
==========
 Tutorial
==========

.. currentmodule:: dugong


Basic Use
=========

A HTTP request can be send and read in four lines::

  with HTTPConnection('www.python.org') as conn:
      conn.send_request('GET', '/index.html')
      resp = conn.read_response()
      body = conn.readall()

`~HTTPConnection.send_request` is a `HTTPResponse` object that gives
access to the response header::

  print('Server said:')
  print('%03d %s' % (resp.status, resp.reason))
  for (key, value) in resp.headers.items():
      print('%s: %s' % (key, value))

`HTTPConnection.readall` returns a a :term:`bytes-like object`. To
convert to text, you could do something like ::

    hit = re.match(r'^(.+?)(?:; charset=(.+))?$', resp.headers['Content-Type'])
    if not hit:
        raise SystemExit("Can't determine response charset")
    elif hit.group(2): # found explicity charset
        charset = hit.group(2)
    elif hit.group(1).startswith('text/'):
        charset = 'latin1' # default for text/ types by RFC 2616
    else:
        raise SystemExit('Server sent binary data')
    text_body = body.decode(charset)


SSL Connections
===============

If you would like to establish a secure connection, you need to pass
the appropriate `~ssl.SSLContext` object to `HTTPConnection`. For example::

    ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    ssl_context.options |= ssl.OP_NO_SSLv2
    ssl_context.verify_mode = ssl.CERT_REQUIRED
    ssl_context.set_default_verify_paths()

    with HTTPConnection('www.google.com', ssl_context=ssl_context) as conn:
        conn.send_request('GET', '/index.html')
        resp = conn.read_response()
        body = conn.readall()

If you need information about the peer certificate, use the
`~HTTPConnection.get_ssl_peercert` method.

Streaming API
=============

When retrieving larger objects, it's generally better not to read the
response body all at once but in smaller chunks::

  BUFSIZE = 32*1024 # Read in 32 kB chunks

  # ...

  conn.send_request('GET', '/big_movie.mp4')
  resp = conn.read_response()
  assert resp.status == 200

  with open('big_movie.mp4', 'wb') as fh:
      while True:
          buf = conn.read(BUFSIZE)
          if not buf:
             break
          fh.write(buf)

Alternatively, the `~HTTPConnection.readinto` method may give better
performance in some situations::

  buf = bytearray(BUFSIZE)
  with open('big_movie.mp4', 'wb') as fh:
      while True:
          len_ = conn.readinto(buf)
          if not len_:
             break
          fh.write(buf[:len_])


Uploading Data
==============

If you want to send data to the server, you can provide the data
directly to `~HTTPConnection.send_request`, ::

  # A simple SQL injection attack for your favorite PHP script
  request_body = "'; DELETE FROM passwords;".encode('us-ascii')
  with HTTPConnection('somehost.com') as conn:
      conn.send_request('POST', '/form.php', body=request_body)
      conn.read_response()

or (if you want to send bigger amounts) you can provide it in multiple
chunks::

  # ...
  with open('newest_release.mp4', r'b') as fh:
      size = os.fstat(fh.fileno()).st_size
      conn.send_request('PUT', '/public/newest_release.mp4',
                        body=BodyFollowing(size))

      while True:
          buf = fh.read(BUFSIZE)
          if not buf:
              break
          conn.write(buf)

  resp = conn.read_response()
  assert resp.status in (200, 204)

Here we used the special `BodyFollowing` class to indicate that the
request body data will be provided in separate calls.

100-Continue Support
====================

When having to transfer large amounts of request bodies to the server,
you typically do not want to sent all the data over the network just
to find out that the server rejected the request because of
e.g. insufficient permissions. To avoid this situation, HTTP 1.1
specifies the *100-continue* mechanism. When using 100-continue, the
client transmits an additional ``Expect: 100-continue`` request
header, and then waits for the server to reply with status ``100
Continue`` before sending the request body data. If the server instead
responds with an error, the client can avoid pointless transmission of
the request body.

To use this mechanism, pass the *expect100* parameter to
`~HTTPConnection.send_request`, and call
`~HTTPConnection.read_response` twice: once before sending body data,
and a second time to read the final response::

  # ...
  with open('newest_release.mp4', r'b') as fh:
      size = os.fstat(fh.fileno()).st_size
      conn.send_request('PUT', '/public/newest_release.mp4',
                        body=BodyFollowing(size), expect100=True)

      resp = conn.read_response()
      if resp.status != 100:
          raise RuntimeError('Server said: %s' % resp.reason)

      while True:
          buf = fh.read(BUFSIZE)
          if not buf:
              break
          conn.write(buf)

  resp = conn.read_response()
  assert resp.status in (200, 204)


Retrying on Error
=================

Sometimes the connection to the remote server may get interrupted for
a variety of reasons, resulting in a variety of exceptions. For
convience, you may use the `is_temp_network_error` method to determine if a
given exception indicates a temporary problem (i.e., if it makes sense
to retry)::

  delay = 1
  conn = HTTPConnection('www.python.org')
  while True:
      try:
          conn.send_request('GET', '/index.html')
          conn.read_response()
          body = conn.readall()
      except Exception as exc:
          if is_temp_network_error(exc):
              print('Got %s, retrying..' % exc)
              time.sleep(delay)
              delay *= 2
          else:
              raise
      else:
          break
      finally:
          conn.disconnect()


Timing out
==========

It can take quite a long time before the operation system recognises
that a TCP/IP connection has been interrupted. If you'd rather be
informed right away when there has been no data exchange for some
period of time, dugong allows you to specify a custom timeout::

  conn = HTTPConnection('www.python.org')
  conn.timeout = 10
  try:
      conn.send_request('GET', '/index.html')
      conn.read_response()
      body = conn.readall()
  except ConnectionTimedOut:
      print('Unable to send or receive any data for more than',
            conn.timeout, 'seconds, aborting.')
      sys.exit(1)


.. _pipelining:

Pipelining with Threads
=======================

Pipelining means sending multiple requests in succession, without
waiting for the responses. First, let's consider how do **not** do it::

  # DO NOT DO THIS!
  conn = HTTPConnection('somehost.com')
  for path in path_list:
      conn.send_request('GET', path)

  bodies = []
  for path in path_list:
      resp = conn.read_response()
      assert resp.status == 200
      bodies.append(conn.readall())

This will probably even work as long as you don't have too many
elements in *path_list*. However, it is very bad practice, because at
some point the server will stop reading requests until some responses
have been read (because all the TCP buffers are full). At this point,
your application will deadlock.

One better way do it is to use threads. Dugong is not generally
threadsafe, but using one thread to send requests and one thread to
read responses is supported::

  with HTTPConnection('somehost.com') as conn:

      def send_requests():
          for path in path_list:
              conn.send_request('GET', path)
      thread = threading.thread(target=send_requests)
      thread.run()

      bodies = []
      for path in path_list:
          resp = conn.read_response()
          assert resp.status == 200
          bodies.append(conn.readall())

      thread.join()

Another way is to use coroutines. This is explained in the next
section.


.. _coroutine_pipelining:

Pipelining with Coroutines
==========================

Instead of using two threads to send requests and responses, you can
also use two coroutines. A coroutine is essentially a function that
can be suspended and resumed at specific points. Dugong coroutines
suspend themself when they would have to wait for an I/O operation to
complete. This makes them perfect for pipelining: we'll define one
coroutine that sends requests, and a second one to read responses, and
then execute them "interleaved": whenever we can't send another
request, we try to read a response, and if we can't read a response,
we try to send another request.

The following example demonstrates how to do this to efficiently
retrieve a large number of documents (stored in *url_list*):

.. literalinclude:: ../examples/pipeline1.py
   :start-after: start-example
   :end-before: end-example

Here we have used the :ref:`yield from expression <yieldexpr>` to
integrate the coroutines returned by
`~HTTPConnection.co_send_request`, `~HTTPConnection.co_read_response`,
and `~HTTPConnection.co_readall` into two custom coroutines
*send_requests* and *read_responses*. To schedule the coroutines, we
use `AioFuture` to obtain `asyncio Futures <asyncio.Future>` for them,
and then rely on the :mod:`asyncio` module to do the heavy lifting and
switch execution between them at the right times.

For more details about this, take a look at :ref:`coroutines`, or the
`asyncio documentation <asyncio>`.