<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[HTTP requests2: how to deal with server that keeps the connection alive?]]></title><description><![CDATA[<p dir="auto">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:</p>
<pre><code>*   Trying 192.168.10.22:80...
* Connected to 192.168.10.22 (192.168.10.22) port 80
&gt; GET /api/v1/data HTTP/1.1
&gt; Host: 192.168.10.22
&gt; User-Agent: curl/8.4.0
&gt; Accept: */*
&gt; Connection: close
&gt; Content-Type: application/json
&gt; 
&lt; HTTP/1.1 200 OK
&lt; Content-Type: application/json
&lt; Content-Length: 1178
&lt; Access-Control-Allow-Origin: *
&lt; 
* 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"}]}%
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">If I add a timeout to the request, like so:</p>
<pre><code>http_req = requests2.get('http://192.168.10.22/api/v1/data', headers={'Content-Type': 'application/json', 'Connection': "close"}, timeout=2)
</code></pre>
<p dir="auto">Then that just triggers an error and crashes the application.</p>
<p dir="auto">Many servers keep the connection alive by default, leading to this issue. Is there a way to work around it?</p>
]]></description><link>https://community.m5stack.com/topic/6973/http-requests2-how-to-deal-with-server-that-keeps-the-connection-alive</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 10:23:39 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/6973.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 11 Nov 2024 14:04:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to HTTP requests2: how to deal with server that keeps the connection alive? on Fri, 15 Nov 2024 13:12:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kuriko" aria-label="Profile: kuriko">@<bdi>kuriko</bdi></a> any pointers on how to do that? There's nothing about it in the docs:<br />
<a href="https://uiflow-micropython.readthedocs.io/en/develop/software/requests2.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://uiflow-micropython.readthedocs.io/en/develop/software/requests2.html</a></p>
]]></description><link>https://community.m5stack.com/post/27048</link><guid isPermaLink="true">https://community.m5stack.com/post/27048</guid><dc:creator><![CDATA[streammyevent]]></dc:creator><pubDate>Fri, 15 Nov 2024 13:12:20 GMT</pubDate></item><item><title><![CDATA[Reply to HTTP requests2: how to deal with server that keeps the connection alive? on Fri, 15 Nov 2024 02:21:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/streammyevent" aria-label="Profile: streammyevent">@<bdi>streammyevent</bdi></a><br />
You may try open a session.</p>
]]></description><link>https://community.m5stack.com/post/27039</link><guid isPermaLink="true">https://community.m5stack.com/post/27039</guid><dc:creator><![CDATA[kuriko]]></dc:creator><pubDate>Fri, 15 Nov 2024 02:21:20 GMT</pubDate></item></channel></rss>