ESP32 In MicroPython: Client Sockets |
Written by Harry Fairhead & Mike James | ||||
Tuesday, 12 September 2023 | ||||
Page 3 of 3
SSL Socket-Based HTTPS ClientMany websites now refuse to serve unencrypted data and insist on HTTPS. Fortunately it is very easy to create an HTTPS client as you don’t need to create a digital certificate. If you need to prove to a server that it is indeed you trying to connect then you should install a new certificate and use it. This is exactly the same procedure as installing a new server certificate, see later. MicroPython has a very minimal implementation of the Python ssl module, but it is enough to do what you need to make an HTTPS client or server. It only has a single function wrap_socket which adds encryption, and in some cases authentication, to an existing socket. At the time of writing, MicroPython for the ESP32 doesn’t support authentication. The wrap_socket function is: ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, This takes an existing socket specified by sock and turns it into an encrypted SSL socket. Apart from sock, all its parameters, including keyfile and certfile which specify files containing the certificate and/or key, are optional. All certificates have to be in PEM (Privacy-Enhanced Mail) format. If the key is stored in the certificate then you only need to use certfile. If the key is stored separately from the certificate you need both certfile and keyfile. When True, the server_side parameter sets the behavior appropriate to a server and when False to a client and cert_reqs determines the level of certificate checking, from CERT_NONE, CERT_OPTIONAL to CERT_REQUIRED. The validity of the certificate is only checked if you select CERT_REQUIRED and currently the ESP32 never checks the certificate for validity. ca_certs is a bytes object containing the certificate change to be used to validate the client’s certificate. server_hostname is used to set the server’s hostname so that the certificate can be checked to ascertain that it does belong to the website and to allow the server to present the appropriate certificate if it is hosting multiple sites. do_handshake should be used with non-blocking sockets and when True defers the handshake whereas when False, the function doesn’t return until the handshake is complete. Notice that not all of the full Python wrap_socket parameters are supported and not all TLS and encryption levels work. This means that you will discover that, at the moment, connecting to some websites is difficult, if not impossible. However, in many case it should simply work, as long as the website concerned is not using a cutting edge implementation. The only modification that the previous HTTP client needs to work with an HTTPS website is: import network import socket import ssl from machine import Pin, Timer from time import sleep_ms def setup(country, ssid, key): . . . wifi=setup(country, ssid, key) print("Connected") ai = socket.getaddrinfo("example.com", 443, where the setup function has been omitted. Notice that all we need to do is perform a default wrap_socket after making the connection. The downloaded HTML is now fetched using HTTPS. The sslSock returned by the wrap_socket function only has stream read and write methods.
In chapter but not in this extract
Summary
Programming the ESP32in MicroPythonBy Harry Fairhead & Mike JamesBuy from Amazon. ContentsPreface
<ASIN:187196282X>
Comments
or email your comment to: comments@i-programmer.info To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin. |
||||
Last Updated ( Tuesday, 12 September 2023 ) |