The Pico In MicroPython: Sockets |
Written by Mike James & Harry Fairhead | ||||||||
Monday, 05 August 2024 | ||||||||
Page 4 of 4
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. It only has a single function wrap_socket but it is enough to do what you need to make an HTTPS client or server. What this does is to add encryption and in some cases authentication to an existing socket. At the time of writing MicroPython for the Pico doesn’t support authentication. The wrap_socket function is: ssl.wrap_socket(sock, keyfile=None, certfile=None, This takes an existing socket specified by sock and turns it into an encrypted SSL socket. Apart from sock all of the other parameters, including keyfile and certfile which specify files which contain the certificate and/or key, are optional. All certificates have to be in PEM 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. server_side sets the behavior appropriate to a client (False) or server (True) 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 Pico never checks the certificate for validity. ca_certs is a bytes object contains 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 web site 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 if True defers the handshake . If False, wrap_socket 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. What this means is that you will discover that connecting to some websites is difficult, if not impossible, at the moment. 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 web site is: import network import socket import rp2 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 Raspberry Pi Pico/W In MicroPython Second EditionBy Harry Fairhead & Mike JamesBuy from Amazon. Contents
Also of interest: <ASIN:B0BR8LWYMZ> <ASIN:B0BL1HS3QD> 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.
Comments
or email your comment to: comments@i-programmer.info |
||||||||
Last Updated ( Tuesday, 06 August 2024 ) |