splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

UnicodeEncodeError when parsing rest response with version 1.6.3 and python 2.7

Open xcgspring opened this issue 8 years ago • 5 comments

Below script works fine with version 1.6.2, but encounter UnicodeEncodeError issue with version 1.6.3, it should leave decoding the bytes to the ElementTree parser instead of decoding the bytes first, check https://stackoverflow.com/questions/21698024/how-to-correctly-parse-utf-8-xml-with-elementtree:

# encoding: utf-8

import time
from splunklib import client

service = client.connect(
    host='10.66.138.6',
    port=8089,
    username='admin',
    password='password',
    app='search',
    owner='nobody'
)

props_conf = service.confs['props']
if 'access_combined' not in props_conf:
    props_conf.create('access_combined')

stanza = props_conf['access_combined']

key = 'EVAL-バイト'
value = 'bytes'
if key not in stanza.content:
    stanza.submit({key: value})

time.sleep(5)
stanza = props_conf['access_combined']
for key, value in stanza.content.items():
    print key, value

Error log:

(venv)localhost:tmp splunk$ python test6.py 
Traceback (most recent call last):
  File "test6.py", line 27, in <module>
    stanza = props_conf['access_combined']
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 1222, in __getitem__
    entries = self._load_list(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 1326, in _load_list
    entries = _load_atom_entries(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 202, in _load_atom_entries
    r = _load_atom(response)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/client.py", line 197, in _load_atom
    return data.load(response.body.read().decode('utf-8'), match)
  File "/Users/splunk/venv/lib/python2.7/site-packages/splunklib/data.py", line 79, in load
    root = XML(text)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1311, in XML
    parser.feed(text)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1657, in feed
    self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3323-3325: ordinal not in range(128)

xcgspring avatar Apr 03 '18 09:04 xcgspring

+1

spellancer avatar May 10 '18 17:05 spellancer

Tracking internally using https://jira.splunk.com/browse/DVPL-7356

skylasam avatar Jun 22 '18 17:06 skylasam

+1

MichaelManz avatar Jul 17 '18 12:07 MichaelManz

I updated SDK to the currently latest v1.6.4 and tested but it doesn't solve the problem as having the same error when reading Japanese characters. RuntimeError at "/opt/splunk/etc/apps/python3_test/bin/splunklib/searchcommands/search_command.py", line 858 : Failed to read transport header: 'ascii' codec can't decode byte 0xef in position 119: ordinal not in range(128)

Cavalierski avatar Sep 19 '18 00:09 Cavalierski

@ xcgspring it looks like this was fixed with #206 in v1.6.5.

@Cavalierski I've followed up with you offline, however the fix in the develop branch seems to sole this issues.

shakeelmohamed avatar Oct 08 '18 20:10 shakeelmohamed