Pulled details of server from internal URL's API using Python with SOAP+XML
Code:
import requests
import xml.etree.ElementTree as ET
headers = {'content-type': 'application/soap+xml'}
body = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<m:Username xmlns:m="http://www.ab.com">test</m:Username>
<m:Password xmlns:m="http://www.ab.com">testpass</m:Password>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:GetCI xmlns:m="http://www.ab.com">
<CIType xsi:type="xsd:string">System</CIType>
<CIName xsi:type="xsd:string">server.example.nl</CIName>
<CIID></CIID>
<AttrFilter xsi:type="xsd:string">PrimaryName+ArpaDomain+SystemStatus+Environment+ServiceLevel+IsVirtual+Coverage</AttrFilter>
<SubObjFilter xsi:type="xsd:string"></SubObjFilter>
</m:GetCI>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
r = requests.post("URL of API", data=body, headers=headers, verify=False)
print(r.text)
SOAP XML response which i got:
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Username xmlns="http://www.ab.com">test</Username><Password xmlns="http://www.hp.com">testpass</Password></soap:Header><soap:Body><ns1:GetCIResponse xmlns:ns1="http://www.ab.com"><returnWord><![CDATA[{"Attributes":{"PrimaryName":"server","ArpaDomain":"example.nl","SystemStatus":"obsolete","Environment":"Development","ServiceLevel":"standard","
IsVirtual":"no","Coverage":"24x7 (00:00-24:00 Mon-Sun)"},"DataCenter":{"DCID":"1041166","SourceID":null,"SourceTool":null},{"InstanceID":"159364108","SolutionName":"oracle engine","SolutionCategory":"engine","InstanceName":null,"InstanceStatus":"deinstalled","InstanceEnvironment":"Test","InstanceImpact":"3 - Multiple Users","InstanceServiceLevel":"standard","
InstanceAvailability":"99.98","InstanceTimezone":"(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna","InstanceCoverage":"11x5 (07:00-18:00 Mon-Fri)","InstanceVersion":null,"InstanceDescription":"Acceptance - Pe
oplesoft Financials Shadow","InstanceAssignmentGroup":"E-INCSSP-APAC-MDR-DBA-EHM","SourceID":null,"SourceTool":null}],"BusinessID":"10292","Owner":"1","User":"1","ACTION":null}],"MaintContracts":[{"ContractName":"123456","ContractStart":null,"ContractEnd":null,"CRCName":null,"SystemHandle":null,"ContractInfo":null,"ACTION":nul
l},{"ContractName":"RedHat Support","ContractStart":null,"ContractEnd":null,"CRCName":null,"SystemHandle":null,"ContractInfo":null,"ACTION":null}],
"Params":[{"ParCd":"AVBM","SrsValue":"Option 3","ACTION":null},{"ParCd":"BILLABLE","SrsValue":"Yes","ACTION":null},{"ParCd":"BT_SERVICE_LEVEL","SrsValue":"Bronze","ACTION":null},{"ParCd":"CPU_TYPE","SrsValue":"Intel(R) Xeon(R) CPU E5530 # 2.40GHz","ACTION":null},{"ParCd":"DRP_PRIORITY","SrsValue":"DR4","ACTION":null},{"ParCd":"RIM_LOAD","SrsValue":"06 JUL 2017 09:41:00","ACTION":null}],
"ABSA":[{"MID":"67260252","MeshID":"4","MeshShortName":"APAC_sdn","MeshName":"AB-SA APAC SDN"}]}]]></returnWord></ns1:GetCIResponse></soap:Body></soap:Envelope>'
Converting string output to Dictionary:
>>> type(r.text)
<class 'str'>
>>> data=json.loads(r.text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dupakunt\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\dupakunt\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\dupakunt\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How to Convert this output to dictionary and extract only "attributes".
import re
root = ET.fromstring(r.text)
for child in root.iter('returnWord'):
text=child.text
l=re.split("[{}]+", text)
details=l[2]
Related
I have an API in my localhost. when I call it on browser, I will see this information:
<string xmlns="http://tempuri.org/">
{"STATUS":"202","STATUS_DT":"98/12/07","CitizenMobile":"091234567","PROFILEKEY":"1233"}
</string>
I want to use this information in my code and parse it as json. my code is:
import json
import requests
url = ""
response = requests.get(url)
print("type of response= ",type(response))
print(response.status_code)
data = response.text
parsed = json.loads(data)
print(parsed)
My output is:
type of response= <class 'requests.models.Response'>
200
Traceback (most recent call last):
File "C:/Users/MN/dev/New Project/form/WebService/TEST_MAZAHERI/Test_Stack.py", line 11, in
<module>
parsed = json.loads(data)
File "C:\Users\MN\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\MN\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\MN\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in
raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I encountered this error : json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Can you help me?
just use response.json to get data in json format
import json
import requests
url = ""
response = requests.get(url)
print("type of response= ",type(response))
print(response.status_code)
data = response.json # changed here
print(data)
Try this. Works fine.
JSON is in XML, get the data, put that into xml library as string and get json from tag.
>>> data = r'<string xmlns="http://tempuri.org/"> {"STATUS":"202","STATUS_DT":"98/12/07","CitizenMobile":"091234567","PROFILEKEY":"1233"}</string>'
>>> from io import StringIO
>>> from lxml import etree
>>> root = etree.parse(StringIO(data))
>>> r = root.getroot()
>>> r.tag #also, you can iterate through particular tag, if xml has multiple tags
'{http://tempuri.org/}string'
>>> r.text #Get json text
' {"STATUS":"202","STATUS_DT":"98/12/07","CitizenMobile":"091234567","PROFILEKEY":"1233"}'
>>>
>>> import json
>>> json.loads(r.text)
{'STATUS': '202', 'STATUS_DT': '98/12/07', 'CitizenMobile': '091234567', 'PROFILEKEY': '1233'}
>>> #further operations add here.
I am getting jsondecode error when I am trying trying to print requests.responce.json() after running the post method.
import requests
import requests_ntlm,json
import sharepy
import warnings
warnings.filterwarnings("ignore")
base_url = 'https://company.sharepoint.com'
folderUrl = 'Shared Documents'
headers = {
"Accept":"application/jason; odata=verbose",
"Content-Type":"application/jason; odata=verbose",
"odata": "verbose",
"X-RequestDigest":"true"
}
r1 = sharepy.connect(base_url,username='user#company.onmicrosoft.com',password='password')
filename = 'testupload.mp4'
request_url = base_url + '/_api/web/getFolderByServerRelativeUrl(\'''Shared Documents''\')/Files/add(url=\'' + filename + '\',overwrite=true)'
k = r1.post(url= base_url +"/_api/contextinfo",headers=headers)
print(k.json())
i am getting below error
Traceback (most recent call last):
File "sharepointextended.py", line 28, in <module>
print(json.loads(k.content.decode('utf-8')))
File
"C:\Users\saipr\AppData\Local\Programs\Python\Python37\lib\json\__init__.py",
line 348, in loads
return _default_decoder.decode(s)
File
"C:\Users\saipr\AppData\Local\Programs\Python\Python37\lib\json\decoder.py",
line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File
"C:\Users\saipr\AppData\Local\Programs\Python\Python37\lib\json\decoder.py",
line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Looks like typo in your request headers.
I am assuming the value should be 'application/json' and not 'application/jason'.
Please ignore this answer if this doesnt help as I am new to python. But, I dont think the post call would have been successful in line with general XMLHTTPRequest implementations.
I'M just playing around to the CISCO switch device using netmiko module and having my credentials like IP address, password , user name in a json file and calling that json file into the python script to get these details.. while executing this i'm getting ad error, i have given the details below, please suggest what i'm doing wrong ...
$ cat CiscoNet_6.py
#!/usr/bin/python3
from __future__ import absolute_import, division, print_function
import netmiko
import paramiko
import json
######################################
## JSON: Javascript object Notation ##
######################################
# creating a tuple
netmiko_exceptions = (netmiko.ssh_exception.NetMikoAuthenticationException,
netmiko.ssh_exception.NetMikoTimeoutException)
with open('devices.json') as dev_file:
devices = json.load(dev_file)
for device in devices:
try:
print('-'*79)
print('Net Device Address Is: -->', device['ip'])
#print('Net Device Address Is: -->', device)
connection = netmiko.ConnectHandler(**device)
print(connection.send_command('show clock'))
print('-'*79)
connection.disconnect()
except netmiko_exceptions as e:
#print('Authentication failed to', 'Device')
print('Failed to', device['ip'], e)
#print('Failed to', device, e)
My JSON File devices.json:
[
{
"ip: "192.168.0.200",
"device_type": "cisco_ios",
"username": "nettest",
"password": "cisco123"
}
]
Error:
Traceback (most recent call last):
File "./CiscoNet_6.py", line 18, in <module>
devices = json.load(dev_file)
File "/python/v3.6.1/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/python/v3.6.1/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/python/v3.6.1/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/python/v3.6.1/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 3 (char 7)
maybe this is why
"ip*"*: "192.168.0.200",
I am trying to print below json output using below script,
Json Sample:
[{"id":"56cd7e4d2d0edcace915e674","protocol":"https","hostName":"127.0.0.1","port":443,"serverName":"Site1","status":"connected","connected":true}]
Code i have used:
import Requests
response = requests.get("https://Site1/rest/settings/all-server-status",params={'serverId': '56cd7e4d2d0edcace915e674'}, verify=False)
json_data = json.loads(response.text)
When i am trying to print json_data i got below error,
Traceback (most recent call last):
File "<pyshell#116>", line 1, in <module>
json_data = json.loads(response.text)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Please help me on this, Thanks in advance!!
I am receiving this error. I am using Python 3.5.0.
Here is my code:
import json
import requests
from tqdm import tqdm
import os.path
r = requests.get("http://prod.ivtr-od.tpg.ch/v1/GetStops.json?key=d95be980-0830-11e5-a039-0002a5d5c51b")
path = "/Users/me/Desktop/JSON/"
pbar = tqdm(r.json()["stops"])
for x in pbar:
tree = {}
fileMON = open(path + x["stopCode"] + ".json", "r", encoding='utf8')
print(fileMON)
if "MON" in json.loads(fileMON()):
tree["MON"] = json.loads(fileMON())["MON"]
and this is the output :
<_io.TextIOWrapper name='/Users/me/Desktop/JSON/31DCdeparts.json' mode='r' encoding='utf8'>
Traceback (most recent call last):
File "json.py", line 14, in <module>
tree["MON"] = json.loads(fileMON())["MON"]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My JSON file is in the UTF8 encoding. Someone can help me ?