Skip to content

ESP32S2 - Order of requests triggers SSL handshake failure #3651

Closed
@jerryneedell

Description

@jerryneedell

Using this test program on a metro_esp32s2 with freshly built main and freshly downloaded adafruit_requests

import ipaddress
import wifi
import adafruit_requests
import time
import ssl
import socketpool

print(wifi.radio.connect('<ssid>', '<password>))

print('ip', wifi.radio.ipv4_address)

ipv4 = ipaddress.ip_address('8.8.8.8')
print('ping', wifi.radio.ping(ipv4))

pool = socketpool.SocketPool(wifi.radio)

https = adafruit_requests.Session(pool, ssl.create_default_context())




TEXT_URL = "https://httpbin.org/get"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

print("Fetching text from %s" % TEXT_URL)
response = https.get(TEXT_URL)
print("-" * 40)

print("Text Response: ", response.text)
print("-" * 40)
response.close()

print("Fetching JSON data from %s" % JSON_GET_URL)
response = https.get(JSON_GET_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)
response.close()

data = "31F"
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = https.post(JSON_POST_URL, data=data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp["data"])
print("-" * 40)
response.close()

json_data = {"Date": "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
response = https.post(JSON_POST_URL, json=json_data)
response = https.post(JSON_POST_URL, json=json_data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)
response.close()

response = https.get("https://api.github.com/repos/adafruit/circuitpython")
print(response.status_code)
print("circuitpython open issues", response.json()["open_issues_count"])
print("circuitpython stars", response.json()["stargazers_count"])
response.close()

it runs successfully

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Hello World!

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.1-192-gec12477f5 on 2020-11-06; Metro ESP32S2 with ESP32S2
>>> import wifi_https
None
ip 10.0.0.30
ping 0.035
Fetching text from https://httpbin.org/get
----------------------------------------
Text Response:  {
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "User-Agent": "Adafruit CircuitPython", 
    "X-Amzn-Trace-Id": "Root=1-5fa5937b-28fbeb523e7a377e74f14ed5"
  }, 
  "origin": "73.61.89.123", 
  "url": "https://httpbin.org/get"
}

----------------------------------------
Fetching JSON data from https://httpbin.org/get
----------------------------------------
JSON Response:  {'url': 'https://httpbin.org/get', 'headers': {'User-Agent': 'Adafruit CircuitPython', 'Host': 'httpbin.org', 'X-Amzn-Trace-Id': 'Root=1-5fa5937b-575ca0ec60c767155d54c2d6'}, 'args': {}, 'origin': '73.61.89.123'}
----------------------------------------
POSTing data to https://httpbin.org/post: 31F
----------------------------------------
Data received from server: 31F
----------------------------------------
POSTing data to https://httpbin.org/post: {'Date': 'July 25, 2019'}
----------------------------------------
JSON Data received from server: {'Date': 'July 25, 2019'}
----------------------------------------
200
circuitpython open issues 337
circuitpython stars 1887
>>> 

But, if I move

response = https.get("https://api.github.com/repos/adafruit/circuitpython")
print(response.status_code)
print("circuitpython open issues", response.json()["open_issues_count"])
print("circuitpython stars", response.json()["stargazers_count"])
response.close()

from the last request to being the first request , then I get this failure on the next request after it.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-rc.1-192-gec12477f5 on 2020-11-06; Metro ESP32S2 with ESP32S2
>>> import wifi_https
None
ip 10.0.0.30
ping 0.047
200
circuitpython open issues 337
circuitpython stars 1887
Fetching text from https://httpbin.org/get
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "wifi_https.py", line 32, in <module>
  File "adafruit_requests.py", line 555, in get
  File "adafruit_requests.py", line 535, in request
  File "adafruit_requests.py", line 429, in _get_socket
  File "adafruit_requests.py", line 425, in _get_socket
OSError: Failed SSL handshake
>>> 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions