ServiceNow - 如何使用SOAP下载报告

时间:2016-05-11 15:36:17

标签: python soap servicenow

我需要从serviceNow自动下载报告。

我已经能够通过以下方法使用pythonselenium以及win32com自动执行此操作。
https://test.service-now.com/sys_report_template.do?CSV&jvar_report_id=92a....7aa

使用selenium访问serviceNow以及修改firefox默认下载选项,将文件转储到windows计算机上的文件夹。

但是,由于所有这些都可以移植到linux服务器,我们希望将其移植到SOAPCURL

我遇到serviceNow herepython个图书馆。

如果我使用ServiceNow.py

中的以下内容列出网站上列出的登录名,密码和实例名,我尝试了以下代码并且代码正常工作
class Change(Base):
    __table__ = 'change_request.do'

并遵循site上列出的客户端脚本。

# Fetch changes updated on the last 5 minutes
changes = chg.last_updated(minutes=5)
#print changes client side script.

for eachline in changes:
    print eachline

但是,当我用sys_report_template.do替换网址时,我收到错误

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\SOAPpy\Parser.py", line 1080, in _parseSOAP
    parser.parse(inpsrc)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "C:\Python27\Lib\xml\sax\xmlreader.py", line 125, in parse
    self.close()
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 220, in close
    self.feed("", isFinal = 1)
  File "C:\Python27\Lib\xml\sax\expatreader.py", line 214, in feed
    self._err_handler.fatalError(exc)
  File "C:\Python27\Lib\xml\sax\handler.py", line 38, in fatalError
    raise exception
SAXParseException: <unknown>:1:0: no element found

这是相关代码

from servicenow import ServiceNow
from servicenow import Connection
from servicenow.drivers import SOAP

# For SOAP connection
conn = SOAP.Auth(username='abc', password='def', instance='test')

rpt  = ServiceNow.Base(conn)
rpt.__table__ = "sys_report_template.do?CSV"

#jvar_report_id replaced with .... to protect confidentiality
report = rpt.fetch_one({'jvar_report_id': '92a6760a......aas'})

for eachline in report:
    print eachline

所以,我的问题是,做这项工作可以做些什么? 我在网上寻找资源和帮助,但没有找到任何。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

经过大量研究后,我能够使用以下方法从csvservicenow格式获取报告。我想我会在这里发帖,以防其他人遇到类似的问题。

import requests
import json

# Set the request parameters
url= 'https://myinstance.service-now.com/sys_report_template.do?CSV&jvar_report_id=929xxxxxxxxxxxxxxxxxxxx0c755'
user = 'my_username'
pwd = 'my_password'

# Set proper headers
headers = {"Accept":"application/json"}

# Do the HTTP request
response = requests.get(url, auth=(user, pwd), headers=headers )
response.raise_for_status()
print response.text

response.text现在有csv格式的报告 我需要接下来弄清楚,如何解析response对象以正确格式提取csv数据。
完成后,我会在这里发帖。但是现在这回答了我的问题。

答案 1 :(得分:0)

我尝试了此操作,并按预期工作。 `导入请求 导入json

url ='https://myinstance.service-now.com/sys_report_template.do?CSV&jvar_report_id=929xxxxxxxxxxxxxxxxxxxx0c755' 用户='my_username' pwd ='my_password'

response = requests.get(URL,auth =(user,pwd),headers = headers)

file_name =“ abc.csv”

以open(file_name,'wb')作为out_file:     out_file.write(response.content) del响应`

相关问题