sseclient icon indicating copy to clipboard operation
sseclient copied to clipboard

decoder setting

Open BG4WCE opened this issue 4 years ago • 5 comments

Hi, I use sseclient to send a request to https://api.trafikinfo.trafikverket.se/v2/data.json, and the got the following error

decoder = lookup(encoding).incrementaldecoder

TypeError: lookup() argument must be str, not None

But when I am looking into the test scripts or examples from programcreek.com, I don't see any example were setting encoder or decoder in the request.

Can someone help? Thanks a lot.

BG4WCE avatar Mar 31 '20 15:03 BG4WCE

Hi, sorry to hear you're having a problem. I'm not following your description of it though. Can you provide a code snippet of what you're trying to do? Are you sure that endpoint you linked provides an SSE stream?

btubbs avatar May 09 '20 04:05 btubbs

@btubbs Hello. I can confirm that it happens also for me.

Error

  File "/builds/Sse.py", line XYZ, in ASDASDASDASD
    for event in self._client:
  File "/usr/lib/python3.8/site-packages/sseclient.py", line 100, in __next__
    self._connect()
  File "/usr/lib/python3.8/site-packages/sseclient.py", line 58, in _connect
    self.decoder = codecs.getincrementaldecoder(
  File "/usr/lib/python3.8/codecs.py", line 995, in getincrementaldecoder
    decoder = lookup(encoding).incrementaldecoder
TypeError: lookup() argument must be str, not None

Where:

        self._client = SSEClient(url, retry=0, verify=False)
        self._client.resp.raise_for_status()

Seems like it is problem with https://github.com/btubbs/sseclient/blob/bb36b9e273e51fbbf15212cf3d17e464cc6e04c9/sseclient.py#L59

Where: self.resp.encoding seems to be None https://2.python-requests.org/en/master/api/#requests.Response.encoding

The server's endpoint

Java Spring SseEmiter is being used as a server. In this case it works via Nginx:

HTTP/1.1 200 
Server: nginx/1.18.0
Date: Thu, 14 May 2020 01:05:36 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive

That is a Header response for the SSE end-point.

What I have noticed

There is a lot of dependencies, but regarding changes where SSEClient was working I can conclude that:

  • sseclient-0.0.24 was passing the tests
  • sseclient-0.0.26 fails while showing the error above

Exploring the repo

Commit https://github.com/btubbs/sseclient/commit/2aff29003e6372b6ec19d1aac75e59d396d67d74 has added this line without ensuring that passed parameter is not None

Guesses

Most likely requests library is not preparing its Response object properly despite charset present in HTTP server Response, so it is being left with None value in its .encoding property.

patrolez avatar May 14 '20 01:05 patrolez

Hey did you solve it? I am having the same issue here using an http client thatr needs to comunicate with a Leshan LWM2M Server

andreafresa avatar Jun 12 '20 15:06 andreafresa

@andreafresa in my case sseclient-0.0.24 was enough to make everything work properly, so I have pinned this specific version to my project.

patrolez avatar Jun 12 '20 15:06 patrolez

This issue was resolved with PR #51

cyprienc avatar Sep 24 '20 08:09 cyprienc