HTTP requests2: how to deal with server that keeps the connection alive?
-
I'm trying to extract current power usage information from a P1 device with an API, but the server is poorly written and doesn't respond to the "Connection: close" header, keeping the connection alive no matter what:
* Trying 192.168.10.22:80... * Connected to 192.168.10.22 (192.168.10.22) port 80 > GET /api/v1/data HTTP/1.1 > Host: 192.168.10.22 > User-Agent: curl/8.4.0 > Accept: */* > Connection: close > Content-Type: application/json > < HTTP/1.1 200 OK < Content-Type: application/json < Content-Length: 1178 < Access-Control-Allow-Origin: * < * Connection #0 to host 192.168.10.22 left intact {"wifi_ssid":"(redacted)","wifi_strength":62,"smr_version":50,"meter_model":"ISKRA 2M550T-1011","unique_id":"(redacted)","active_tariff":2,"total_power_import_kwh":118615.868,"total_power_import_t1_kwh":31059.650,"total_power_import_t2_kwh":87556.218,"total_power_export_kwh":0.016,"total_power_export_t1_kwh":0.002,"total_power_export_t2_kwh":0.014,"active_power_w":5921.000,"active_power_l1_w":1424.000,"active_power_l2_w":1449.000,"active_power_l3_w":3074.000,"active_voltage_l1_v":226.400,"active_voltage_l2_v":225.100,"active_voltage_l3_v":226.700,"active_current_a":26.287,"active_current_l1_a":6.290,"active_current_l2_a":6.437,"active_current_l3_a":13.560,"voltage_sag_l1_count":7.000,"voltage_sag_l2_count":6.000,"voltage_sag_l3_count":7.000,"voltage_swell_l1_count":1.000,"voltage_swell_l2_count":1.000,"voltage_swell_l3_count":1.000,"any_power_fail_count":6.000,"long_power_fail_count":11.000,"total_gas_m3":13682.241,"gas_timestamp":241111145004,"gas_unique_id":"(redacted)","external":[{"unique_id":"(redacted)","type":"gas_meter","timestamp":241111145004,"value":13682.241,"unit":"m3"}]}%This causes request2 to freeze when trying to get the response contents, like when calling 'print(http_req.text)'. Performing a GET request on any other server works fine, so it's pretty clear to me that this is the issue.
If I add a timeout to the request, like so:
http_req = requests2.get('http://192.168.10.22/api/v1/data', headers={'Content-Type': 'application/json', 'Connection': "close"}, timeout=2)Then that just triggers an error and crashes the application.
Many servers keep the connection alive by default, leading to this issue. Is there a way to work around it?
-
@streammyevent
You may try open a session. -
@kuriko any pointers on how to do that? There's nothing about it in the docs:
https://uiflow-micropython.readthedocs.io/en/develop/software/requests2.html
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login