This file is indexed.

/usr/share/doc/python-pycurl-doc/html/quickstart.html is in python-pycurl-doc 7.43.0-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PycURL Quick Start &mdash; PycURL 7.43.0 documentation</title>
    
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '7.43.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/favicon.ico"/>
    <link rel="top" title="PycURL 7.43.0 documentation" href="index.html" />
    <link rel="next" title="pycurl Module Functionality" href="pycurl.html" />
    <link rel="prev" title="PycURL Installation" href="install.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pycurl.html" title="pycurl Module Functionality"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="install.html" title="PycURL Installation"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PycURL 7.43.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="pycurl-quick-start">
<h1>PycURL Quick Start<a class="headerlink" href="#pycurl-quick-start" title="Permalink to this headline"></a></h1>
<div class="section" id="retrieving-a-network-resource">
<h2>Retrieving A Network Resource<a class="headerlink" href="#retrieving-a-network-resource" title="Permalink to this headline"></a></h2>
<p>Once PycURL is installed we can perform network operations. The simplest
one is retrieving a resource by its URL. To issue a network request with
PycURL, the following steps are required:</p>
<blockquote>
<div><ol class="arabic simple">
<li>Create a <code class="docutils literal"><span class="pre">pycurl.Curl</span></code> instance.</li>
<li>Use <code class="docutils literal"><span class="pre">setopt</span></code> to set options.</li>
<li>Call <code class="docutils literal"><span class="pre">perform</span></code> to perform the operation.</li>
</ol>
</div></blockquote>
<p>Here is how we can retrieve a network resource in Python 2:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>
<span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span>

<span class="nb">buffer</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/&#39;</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEDATA</span><span class="p">,</span> <span class="nb">buffer</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="n">body</span> <span class="o">=</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="c1"># Body is a string in some encoding.</span>
<span class="c1"># In Python 2, we can print it without knowing what the encoding is.</span>
<span class="k">print</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/get_python2.py</span></code>.</p>
<p>PycURL does not provide storage for the network response - that is the
application&#8217;s job. Therefore we must setup a buffer (in the form of a
StringIO object) and instruct PycURL to write to that buffer.</p>
<p>Most of the existing PycURL code uses WRITEFUNCTION instead of WRITEDATA
as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEFUNCTION</span><span class="p">,</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">write</span><span class="p">)</span>
</pre></div>
</div>
<p>While the WRITEFUNCTION idiom continues to work, it is now unnecessary.
As of PycURL 7.19.3 WRITEDATA accepts any Python object with a <code class="docutils literal"><span class="pre">write</span></code>
method.</p>
<p>Python 3 version is slightly more complicated:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>
<span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>

<span class="nb">buffer</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/&#39;</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEDATA</span><span class="p">,</span> <span class="nb">buffer</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="n">body</span> <span class="o">=</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="c1"># Body is a byte string.</span>
<span class="c1"># We have to know the encoding in order to print it to a text file</span>
<span class="c1"># such as standard output.</span>
<span class="k">print</span><span class="p">(</span><span class="n">body</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;iso-8859-1&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/get_python3.py</span></code>.</p>
<p>In Python 3, PycURL response the response body as a byte string.
This is handy if we are downloading a binary file, but for text documents
we must decode the byte string. In the above example, we assume that the
body is encoded in iso-8859-1.</p>
<p>Python 2 and Python 3 versions can be combined. Doing so requires decoding
the response body as in Python 3 version. The code for the combined
example can be found in <code class="docutils literal"><span class="pre">examples/quickstart/get.py</span></code>.</p>
</div>
<div class="section" id="troubleshooting">
<h2>Troubleshooting<a class="headerlink" href="#troubleshooting" title="Permalink to this headline"></a></h2>
<p>When things don&#8217;t work as expected, use libcurl&#8217;s <code class="docutils literal"><span class="pre">VERBOSE</span></code> option to
receive lots of debugging output pertaining to the request:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">VERBOSE</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>It is often helpful to compare verbose output from the program using PycURL
with that of <code class="docutils literal"><span class="pre">curl</span></code> command line tool when the latter is invoked with
<code class="docutils literal"><span class="pre">-v</span></code> option:</p>
<div class="highlight-python"><div class="highlight"><pre>curl -v http://pycurl.io/
</pre></div>
</div>
</div>
<div class="section" id="examining-response-headers">
<h2>Examining Response Headers<a class="headerlink" href="#examining-response-headers" title="Permalink to this headline"></a></h2>
<p>In reality we want to decode the response using the encoding specified by
the server rather than assuming an encoding. To do this we need to
examine the response headers:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> <span class="k">as</span> <span class="n">BytesIO</span>

<span class="n">headers</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">def</span> <span class="nf">header_function</span><span class="p">(</span><span class="n">header_line</span><span class="p">):</span>
    <span class="c1"># HTTP standard specifies that headers are encoded in iso-8859-1.</span>
    <span class="c1"># On Python 2, decoding step can be skipped.</span>
    <span class="c1"># On Python 3, decoding step is required.</span>
    <span class="n">header_line</span> <span class="o">=</span> <span class="n">header_line</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;iso-8859-1&#39;</span><span class="p">)</span>

    <span class="c1"># Header lines include the first status line (HTTP/1.x ...).</span>
    <span class="c1"># We are going to ignore all lines that don&#39;t have a colon in them.</span>
    <span class="c1"># This will botch headers that are split on multiple lines...</span>
    <span class="k">if</span> <span class="s1">&#39;:&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">header_line</span><span class="p">:</span>
        <span class="k">return</span>

    <span class="c1"># Break the header line into header name and value.</span>
    <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">header_line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

    <span class="c1"># Remove whitespace that may be present.</span>
    <span class="c1"># Header lines include the trailing newline, and there may be whitespace</span>
    <span class="c1"># around the colon.</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

    <span class="c1"># Header names are case insensitive.</span>
    <span class="c1"># Lowercase name here.</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="c1"># Now we can actually record the header name and value.</span>
    <span class="n">headers</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

<span class="nb">buffer</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io&#39;</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEFUNCTION</span><span class="p">,</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">write</span><span class="p">)</span>
<span class="c1"># Set our header function.</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">HEADERFUNCTION</span><span class="p">,</span> <span class="n">header_function</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="c1"># Figure out what encoding was sent with the response, if any.</span>
<span class="c1"># Check against lowercased header name.</span>
<span class="n">encoding</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">if</span> <span class="s1">&#39;content-type&#39;</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span>
    <span class="n">content_type</span> <span class="o">=</span> <span class="n">headers</span><span class="p">[</span><span class="s1">&#39;content-type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;charset=(\S+)&#39;</span><span class="p">,</span> <span class="n">content_type</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
        <span class="n">encoding</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s1">&#39;Decoding using </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">encoding</span><span class="p">)</span>
<span class="k">if</span> <span class="n">encoding</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
    <span class="c1"># Default encoding for HTML is iso-8859-1.</span>
    <span class="c1"># Other content types may have different default encoding,</span>
    <span class="c1"># or in case of binary data, may have no encoding at all.</span>
    <span class="n">encoding</span> <span class="o">=</span> <span class="s1">&#39;iso-8859-1&#39;</span>
    <span class="k">print</span><span class="p">(</span><span class="s1">&#39;Assuming encoding is </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">encoding</span><span class="p">)</span>

<span class="n">body</span> <span class="o">=</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="c1"># Decode using the encoding we figured out.</span>
<span class="k">print</span><span class="p">(</span><span class="n">body</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">encoding</span><span class="p">))</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/response_headers.py</span></code>.</p>
<p>That was a lot of code for something very straightforward. Unfortunately,
as libcurl refrains from allocating memory for response data, it is on our
application to perform this grunt work.</p>
</div>
<div class="section" id="writing-to-a-file">
<h2>Writing To A File<a class="headerlink" href="#writing-to-a-file" title="Permalink to this headline"></a></h2>
<p>Suppose we want to save response body to a file. This is actually easy
for a change:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>

<span class="c1"># As long as the file is opened in binary mode, both Python 2 and Python 3</span>
<span class="c1"># can write response body to it without decoding.</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;out.html&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
    <span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/&#39;</span><span class="p">)</span>
    <span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEDATA</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
    <span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
    <span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/write_file.py</span></code>.</p>
<p>The important part is opening the file in binary mode - then response body
can be written bytewise without decoding or encoding steps.</p>
</div>
<div class="section" id="following-redirects">
<h2>Following Redirects<a class="headerlink" href="#following-redirects" title="Permalink to this headline"></a></h2>
<p>By default libcurl, and PycURL, do not follow redirects. Changing this
behavior involves using <code class="docutils literal"><span class="pre">setopt</span></code> like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="c1"># Redirects to https://www.python.org/.</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://www.python.org/&#39;</span><span class="p">)</span>
<span class="c1"># Follow redirect.</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">FOLLOWLOCATION</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/follow_redirect.py</span></code>.</p>
<p>As we did not set a write callback, the default libcurl and PycURL behavior
to write response body to standard output takes effect.</p>
</div>
<div class="section" id="setting-options">
<h2>Setting Options<a class="headerlink" href="#setting-options" title="Permalink to this headline"></a></h2>
<p>Following redirects is one option that libcurl provides. There are many more
such options, and they are documented on <a class="reference external" href="http://curl.haxx.se/libcurl/c/curl_easy_setopt.html">curl_easy_setopt</a> page.
With very few exceptions, PycURL option names are derived from libcurl
option names by removing the <code class="docutils literal"><span class="pre">CURLOPT_</span></code> prefix. Thus, <code class="docutils literal"><span class="pre">CURLOPT_URL</span></code>
becomes simply <code class="docutils literal"><span class="pre">URL</span></code>.</p>
</div>
<div class="section" id="examining-response">
<h2>Examining Response<a class="headerlink" href="#examining-response" title="Permalink to this headline"></a></h2>
<p>We already covered examining response headers. Other response information is
accessible via <code class="docutils literal"><span class="pre">getinfo</span></code> call as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">StringIO</span> <span class="kn">import</span> <span class="n">StringIO</span> <span class="k">as</span> <span class="n">BytesIO</span>

<span class="nb">buffer</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/&#39;</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">WRITEDATA</span><span class="p">,</span> <span class="nb">buffer</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>

<span class="c1"># HTTP response code, e.g. 200.</span>
<span class="k">print</span><span class="p">(</span><span class="s1">&#39;Status: </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">c</span><span class="o">.</span><span class="n">getinfo</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">RESPONSE_CODE</span><span class="p">))</span>
<span class="c1"># Elapsed time for the transfer.</span>
<span class="k">print</span><span class="p">(</span><span class="s1">&#39;Status: </span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">c</span><span class="o">.</span><span class="n">getinfo</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">TOTAL_TIME</span><span class="p">))</span>

<span class="c1"># getinfo must be called before close.</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/response_info.py</span></code>.</p>
<p>Here we write the body to a buffer to avoid printing uninteresting output
to standard out.</p>
<p>Response information that libcurl exposes is documented on
<a class="reference external" href="http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html">curl_easy_getinfo</a> page. With very few exceptions, PycURL constants
are derived from libcurl constants by removing the <code class="docutils literal"><span class="pre">CURLINFO_</span></code> prefix.
Thus, <code class="docutils literal"><span class="pre">CURLINFO_RESPONSE_CODE</span></code> becomes simply <code class="docutils literal"><span class="pre">RESPONSE_CODE</span></code>.</p>
</div>
<div class="section" id="sending-form-data">
<h2>Sending Form Data<a class="headerlink" href="#sending-form-data" title="Permalink to this headline"></a></h2>
<p>To send form data, use <code class="docutils literal"><span class="pre">POSTFIELDS</span></code> option. Form data must be URL-encoded
beforehand:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>
<span class="k">try</span><span class="p">:</span>
    <span class="c1"># python 3</span>
    <span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlencode</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c1"># python 2</span>
    <span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">urlencode</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/tests/testpostvars.php&#39;</span><span class="p">)</span>

<span class="n">post_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;field&#39;</span><span class="p">:</span> <span class="s1">&#39;value&#39;</span><span class="p">}</span>
<span class="c1"># Form data must be provided already urlencoded.</span>
<span class="n">postfields</span> <span class="o">=</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">post_data</span><span class="p">)</span>
<span class="c1"># Sets request method to POST,</span>
<span class="c1"># Content-Type header to application/x-www-form-urlencoded</span>
<span class="c1"># and data to send in request body.</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">POSTFIELDS</span><span class="p">,</span> <span class="n">postfields</span><span class="p">)</span>

<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/form_post.py</span></code>.</p>
<p><code class="docutils literal"><span class="pre">POSTFIELDS</span></code> automatically sets HTTP request method to POST. Other request
methods can be specified via <code class="docutils literal"><span class="pre">CUSTOMREQUEST</span></code> option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">CUSTOMREQUEST</span><span class="p">,</span> <span class="s1">&#39;PATCH&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="file-upload">
<h2>File Upload<a class="headerlink" href="#file-upload" title="Permalink to this headline"></a></h2>
<p>To upload a file, use <code class="docutils literal"><span class="pre">HTTPPOST</span></code> option. To upload a physical file,
use <code class="docutils literal"><span class="pre">FORM_FILE</span></code> as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/tests/testfileupload.php&#39;</span><span class="p">)</span>

<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">HTTPPOST</span><span class="p">,</span> <span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;fileupload&#39;</span><span class="p">,</span> <span class="p">(</span>
        <span class="c1"># upload the contents of this file</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_FILE</span><span class="p">,</span> <span class="n">__file__</span><span class="p">,</span>
    <span class="p">)),</span>
<span class="p">])</span>

<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/file_upload_real.py</span></code>.</p>
<p><code class="docutils literal"><span class="pre">libcurl</span></code> provides a number of options to tweak file uploads and multipart
form submissions in general. These are documented on <a class="reference external" href="http://curl.haxx.se/libcurl/c/curl_formadd.html">curl_formadd page</a>.
For example, to set a different filename and content type:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/tests/testfileupload.php&#39;</span><span class="p">)</span>

<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">HTTPPOST</span><span class="p">,</span> <span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;fileupload&#39;</span><span class="p">,</span> <span class="p">(</span>
        <span class="c1"># upload the contents of this file</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_FILE</span><span class="p">,</span> <span class="n">__file__</span><span class="p">,</span>
        <span class="c1"># specify a different file name for the upload</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_FILENAME</span><span class="p">,</span> <span class="s1">&#39;helloworld.py&#39;</span><span class="p">,</span>
        <span class="c1"># specify a different content type</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_CONTENTTYPE</span><span class="p">,</span> <span class="s1">&#39;application/x-python&#39;</span><span class="p">,</span>
    <span class="p">)),</span>
<span class="p">])</span>

<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/file_upload_real_fancy.py</span></code>.</p>
<p>If the file data is in memory, use <code class="docutils literal"><span class="pre">BUFFER</span></code>/<code class="docutils literal"><span class="pre">BUFFERPTR</span></code> as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pycurl</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">pycurl</span><span class="o">.</span><span class="n">Curl</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">URL</span><span class="p">,</span> <span class="s1">&#39;http://pycurl.io/tests/testfileupload.php&#39;</span><span class="p">)</span>

<span class="n">c</span><span class="o">.</span><span class="n">setopt</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">HTTPPOST</span><span class="p">,</span> <span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;fileupload&#39;</span><span class="p">,</span> <span class="p">(</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_BUFFER</span><span class="p">,</span> <span class="s1">&#39;readme.txt&#39;</span><span class="p">,</span>
        <span class="n">c</span><span class="o">.</span><span class="n">FORM_BUFFERPTR</span><span class="p">,</span> <span class="s1">&#39;This is a fancy readme file&#39;</span><span class="p">,</span>
    <span class="p">)),</span>
<span class="p">])</span>

<span class="n">c</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>This code is available as <code class="docutils literal"><span class="pre">examples/quickstart/file_upload_buffer.py</span></code>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">PycURL Quick Start</a><ul>
<li><a class="reference internal" href="#retrieving-a-network-resource">Retrieving A Network Resource</a></li>
<li><a class="reference internal" href="#troubleshooting">Troubleshooting</a></li>
<li><a class="reference internal" href="#examining-response-headers">Examining Response Headers</a></li>
<li><a class="reference internal" href="#writing-to-a-file">Writing To A File</a></li>
<li><a class="reference internal" href="#following-redirects">Following Redirects</a></li>
<li><a class="reference internal" href="#setting-options">Setting Options</a></li>
<li><a class="reference internal" href="#examining-response">Examining Response</a></li>
<li><a class="reference internal" href="#sending-form-data">Sending Form Data</a></li>
<li><a class="reference internal" href="#file-upload">File Upload</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="install.html"
                        title="previous chapter">PycURL Installation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="pycurl.html"
                        title="next chapter">pycurl Module Functionality</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/quickstart.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pycurl.html" title="pycurl Module Functionality"
             >next</a> |</li>
        <li class="right" >
          <a href="install.html" title="PycURL Installation"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">PycURL 7.43.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &copy; Copyright 2001-2016 Kjetil Jacobsen, Markus F.X.J. Oberhumer, Oleg Pudeyev.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
    </div>
  </body>
</html>