Splunk Dev

Empty response in Python SDK example a.py ?

smeier
Path Finder

I am working through the Python SDK and getting this error on the first script.

Traceback (most recent call last):
  File "C:\Users\.\Downloads\splunk-sdk-python-1.6.0\examples\abc\a.py", line 49, in <module>
    sessionKey = ElementTree.XML(body).findtext("./sessionKey")
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1312, in XML
    return parser.close()
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1665, in close
    self._raiseerror(v)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1517, in _raiseerror
    raise err
ParseError: no element found: line 1, column 0

I tried printing the contents of 'body' and it is empty.

1 Solution

smeier
Path Finder

It looks like the connection was being closed before the response was being read..

        try:
            connection.request("POST", "/services/auth/login", body, headers)
            response = connection.getresponse()
        finally:
            **connection.close()**
        if response.status != 200:
            raise Exception, "%d (%s)" % (response.status, response.reason)
        **body = response.read()**
        sessionKey = ElementTree.XML(body).findtext("./sessionKey")

If you fix it up a little, it will work..

        try:
            connection.request("POST", "/services/auth/login", body, headers)
            response = connection.getresponse()
        finally:
            **#connection.close()**
            print ''
        if response.status != 200:
            raise Exception, "%d (%s)" % (response.status, response.reason)
        **body = response.read()**
        **connection.close()**
        sessionKey = ElementTree.XML(body).findtext("./sessionKey")

I'm wondering if anyone else has ever used this example 😉

View solution in original post

smeier
Path Finder

It looks like the connection was being closed before the response was being read..

        try:
            connection.request("POST", "/services/auth/login", body, headers)
            response = connection.getresponse()
        finally:
            **connection.close()**
        if response.status != 200:
            raise Exception, "%d (%s)" % (response.status, response.reason)
        **body = response.read()**
        sessionKey = ElementTree.XML(body).findtext("./sessionKey")

If you fix it up a little, it will work..

        try:
            connection.request("POST", "/services/auth/login", body, headers)
            response = connection.getresponse()
        finally:
            **#connection.close()**
            print ''
        if response.status != 200:
            raise Exception, "%d (%s)" % (response.status, response.reason)
        **body = response.read()**
        **connection.close()**
        sessionKey = ElementTree.XML(body).findtext("./sessionKey")

I'm wondering if anyone else has ever used this example 😉

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...