phyphox / phyphox-android

Physical Phone Experiments
GNU General Public License v3.0
319 stars 42 forks source link

REST API not urldecoding pipe char #34

Closed quantenschaum closed 2 years ago

quantenschaum commented 2 years ago

I followed the docs to query phyphox (on android) from python using requests. When trying to retrieve partial data the query string contains something like accX=1.234|acc_time. The pipe | is used in the query string and phyphox requires the pipe not to be urlencoded, otherwise it will not return any data (sometime it does, but most of the time it does not). When using requests the url is always urlencoded (cannot be turned off), which makes the very good and popular requests library unusable with phyphox, unfortunately. My current workaround is to use plain urllib.

It would be great to get this fixed.

code example to reproduce

from requests import get
from time import sleep
base="http://mymobile:8080/"
get(base+"control",{"cmd":"clear"})
get(base+"control",{"cmd":"start"})
for i in range(10):
    sleep(0.1)
    get(base+"get",{"accX":f"{i/10}|acc_time"})
get(base+"control",{"cmd":"stop"})

leads to

RemoteDisconnected                        Traceback (most recent call last)
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    698             # Make the request on the httplib connection object.
--> 699             httplib_response = self._make_request(
    700                 conn,

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    444                     # Otherwise it looks like a bug in the code.
--> 445                     six.raise_from(e, None)
    446         except (SocketTimeout, BaseSSLError, SocketError) as e:

/usr/lib/python3/dist-packages/six.py in raise_from(value, from_value)

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    439                 try:
--> 440                     httplib_response = conn.getresponse()
    441                 except BaseException as e:

/usr/lib/python3.10/http/client.py in getresponse(self)
   1373             try:
-> 1374                 response.begin()
   1375             except ConnectionError:

/usr/lib/python3.10/http/client.py in begin(self)
    317         while True:
--> 318             version, status, reason = self._read_status()
    319             if status != CONTINUE:

/usr/lib/python3.10/http/client.py in _read_status(self)
    286             # sending a valid response.
--> 287             raise RemoteDisconnected("Remote end closed connection without"
    288                                      " response")

RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~/.local/lib/python3.10/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    488             if not chunked:
--> 489                 resp = conn.urlopen(
    490                     method=request.method,

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    754 
--> 755             retries = retries.increment(
    756                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

/usr/lib/python3/dist-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    531             if read is False or not self._is_method_retryable(method):
--> 532                 raise six.reraise(type(error), error, _stacktrace)
    533             elif read is not None:

/usr/lib/python3/dist-packages/six.py in reraise(tp, value, tb)
    717             if value.__traceback__ is not tb:
--> 718                 raise value.with_traceback(tb)
    719             raise value

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    698             # Make the request on the httplib connection object.
--> 699             httplib_response = self._make_request(
    700                 conn,

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    444                     # Otherwise it looks like a bug in the code.
--> 445                     six.raise_from(e, None)
    446         except (SocketTimeout, BaseSSLError, SocketError) as e:

/usr/lib/python3/dist-packages/six.py in raise_from(value, from_value)

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    439                 try:
--> 440                     httplib_response = conn.getresponse()
    441                 except BaseException as e:

/usr/lib/python3.10/http/client.py in getresponse(self)
   1373             try:
-> 1374                 response.begin()
   1375             except ConnectionError:

/usr/lib/python3.10/http/client.py in begin(self)
    317         while True:
--> 318             version, status, reason = self._read_status()
    319             if status != CONTINUE:

/usr/lib/python3.10/http/client.py in _read_status(self)
    286             # sending a valid response.
--> 287             raise RemoteDisconnected("Remote end closed connection without"
    288                                      " response")

ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
Staacks commented 2 years ago

Fixed in the next update via 471acd4.

I wish every issue was like that: Detailed description of the issue, a little script to simply test it and just a single line changed to fix it. Thanks for reporting this.