SSL connection

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
polo0000
Posts: 2
Joined: Fri Oct 04, 2019 10:51 pm

SSL connection

Post by polo0000 » Fri Oct 04, 2019 11:01 pm

Hi,

For my projet i need use https connection with socket and ssl module. But when i run this command :
ssl.wrap_socket(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None)
return -> CERT_NONE isn't defined
And when i test ssl.CERT_NONE
return -> AttributeError: 'module' object has no attribute 'CERT_NONE'

My script :

Code: Select all

import socket
import ussl

def http_get(url):
	_, _, host, path = url.split('/', 3)
	addr = socket.getaddrinfo(host, 443)[0][-1]
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect(addr)
	s = ussl.wrap_socket(s, server_side=False, keyfile=None, certfile=None, cert_reqs=CERT_NONE, ca_certs=None)
	s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
	while True:
		data = s.recv(100)
		if data:
			print(str(data, 'utf8'), end='')
		else:
			break
	s.close()
I run on a ESP8266 with latest version of microPython -> esp8266-20190529-v1.11.bin

Do you have the same issue.

Thank's for your help.

User avatar
jimmo
Posts: 648
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: SSL connection

Post by jimmo » Sun Oct 06, 2019 2:27 am

Unfortunately the documentation isn't super clear here -- cert_reqs only applies the CC3200 (i.e. Pycom WiPy) port. Therefore the CERT_NONE constant is only available on this port too.

On axtls and mbedtls ports (including ESP8266), the following arguments to wrap_socket are supported: key, cert, server_side, server_hostname, do_handshake.

For more info about the ways in which wrap_socket is unfortunately different between ports, see this thread: viewtopic.php?f=2&t=6499&p=37058#p37058 (But sounds like you don't need cert validation, so hopefully none of this matters to you!)

polo0000
Posts: 2
Joined: Fri Oct 04, 2019 10:51 pm

Re: SSL connection

Post by polo0000 » Tue Oct 08, 2019 8:52 pm

Thank you very much for your answer!

With your help the ussl.wrap_socket(s) work but i have another issue with s.send()

Finaly i use urllib.request.urlopen() and it's work very well.

Post Reply