Skip to content
Boris Lovosevic edited this page Jan 22, 2018 · 7 revisions

Curl module

This module uses libcurl to implement various client protocols.

Features

  • http and https
    • get send GET request to the http/https server, get response to string or file
    • post send OST` request to the http/https server, posted data can be numbers, strings or files
  • ftp
    • get, put and list commands are implemented
  • smtp
    • sendmail send mail, gMail supported, multiple attachments supported

Note:
If TLS is enabled, the module uses mbedTLS and more memory is required.
If not using psRAM, you may need to lower the MicroPython heap (72 KB will usually be enough).
You may try to set
→ Component config → mbedTLS → TLS maximum message content length
to a lower value than default 16 kB, for example to 4 kB. It will usually work, but The TLS may fail if the server side does not support TLS Maximum Fragment Length Negotiation Extension.



Methods


curl.options(print, verbose, progress, timeout, maxfsize, hdrlen, bodylen, nodecode)

Get or set curl options.
All arguments are optional. All, except ṗrint, arguments are kw arguments and must be entered in arg=value format.

Arg Description
print Default: True; print the current options.
False can be used when setting the option(s) from Python script
verbose if True, detailed report about curl operations is printed
progress if True, prints the progress during the file upload/download
timeout Default: 60 sek; set the timeout curl operations in seconds. Minimal value allowed is 10 seconds
maxfsize Default: 300000
Maximum download file size. If the requested file size is bigger, the whole file will be downloaded, but the file on filesystem will be truncated at that value.
hdrlen Default: 512 or 1024 if psRAM is used
The size of the header buffer. Header buffer is returned as the 2nd item in curl operations result and contains the http header or other information
bodylen Default: 1024 or 4096 if psRAM is used
The size of the body buffer. Body buffer is returned as 3rd item in curl operations result and contains the response from the server.
nodecode if set to True, do not allow using compression in http requests
>>> import curl
>>> curl.options()
Curl options(
  Verbose: True, Progress: 0, Timeout: 60, Max fsize: 300000, Header len: 1024, Body len: 4096, Decode content: True
)
>>> 

curl.info()

Print information about libcurl version and supported protocols

>>> curl.info()
Curl Info (
  Curl version info
    version: 7.58.0-DEV - 473600
  Host: MicroPython-ESP32
    - IP V6 supported
    - SSL supported
    - LIBZ supported
    - DEBUG NOT supported
  Protocols:
    - ftp
    - ftps
    - http
    - https
    - imap
    - imaps
    - scp
    - sftp
    - smtp
    - smtps
)
>>> 

curl.get(url [, file])

Send GET request to http/https server.

url is the reference to the http resource
It can contain the parameters to be passed to the server.

file argument is optional, if not used, the file/request result will be downloaded into response buffer (and truncated to its size).
If used, the remote file will be downloaded to the file on MicroPython file system.
Special file name can be used: simulate. The file will be downloaded, but no actual saving to file will occur.

The method returns tuple of 3 items: (res_code, hdr, body)
On succes res_code will have the value 0, on fail the error code.

>>> res = curl.get('loboris.eu/ESP32/info.txt')
>>> res[0]
0
>>> print(res[1])
HTTP/1.1 200 OK
Date: Mon, 22 Jan 2018 12:36:24 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 05 Oct 2017 11:08:15 GMT
ETag: "181-55acabda6ed78-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 267
Content-Type: text/plain


>>> print(res[2])
================
Welcome to ESP32
================

The ESP32 is a low cost, low power microcontroller
with integrated Wi-Fi & dual-mode Bluetooth,
which employs a dual-core
Tensilica Xtensa LX6 microprocessor.

ESP32 is created and developed by Espressif Systems,
a Shanghai-based Chinese company,
and is manufactured by TSMC using their 40 nm process.

---------
2017 LoBo
---------
>>> res = curl.get("loboris.eu/ESP32/test.php?param1=12345&param2=Hi_from_MicroPython&temper=8.34")
>>> print(res[1])
HTTP/1.1 200 OK
Date: Mon, 22 Jan 2018 12:48:39 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.22
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 241
Content-Type: text/html


>>> print(res[2])
============
Method: GET
============

--------------------------
Number of uploaded files: 0
    Number of GET params: 3
   Number of POST params: 0
--------------------------

===========
Debug info:
===========

-----------
POST DATA: 
-----------
Array
(
)
-----------
 GET DATA:  
-----------
Array
(
    [param1] => 12345
    [param2] => Hi_from_MicroPython
    [temper] => 8.34
)
-----------
FILE DATA: 
-----------
Array
(
)
-----------

=====================================
LoBo test server, 2018/01/22 13:48:39
=====================================

>>> 

curl.post(url, params)

Send POST request to http/https server.

url is the reference to the http resource

params is dictionary containing the data to be posted to the server.
The dictionary values can be integers, floats, strings or file names.

>>> param = {'Name': 'Micro', 'temper': 12.34, 'code': 8367, 'file': 'boot.py'}
>>> res = curl.post("loboris.eu/ESP32/test.php", param)
>>> res[0]
0
>>> print(res[1])
HTTP/1.1 200 OK
Date: Mon, 22 Jan 2018 13:21:57 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.22
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 317
Content-Type: text/html


>>> print(res[2])
============
Method: POST
============

--------------------------
Number of uploaded files: 1
    Number of GET params: 0
   Number of POST params: 3
--------------------------

===========
Debug info:
===========

-----------
POST DATA: 
-----------
Array
(
    [code] => 8367
    [Name] => Micro
    [temper] => 12.340000
)
-----------
 GET DATA:  
-----------
Array
(
)
-----------
FILE DATA: 
-----------
Array
(
    [file] => Array
        (
            [name] => boot.py
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpCxLKoj
            [error] => 0
            [size] => 113
        )

)
-----------

=====================================
LoBo test server, 2018/01/22 14:21:57
=====================================

>>> 
Clone this wiki locally