Python web services client examples
Summarize
Summarized using AI
This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.
Summary of Python web services client examples
This content provides practical Python examples demonstrating integration with ServiceNow web services using SOAP. It guides ServiceNow customers through common tasks such as creating incidents, querying records, and handling advanced event-driven workflows by interacting with ServiceNow's SOAP API via the SOAPpy library.
Show less
Requirements
- Python modules required: fpconst, PyXML, and SOAPpy.
- Use HTTPS URLs formatted as
https://INSTANCE.service-now.comrather than using www in the URL.
Key Examples
- Insert Incident: Shows how to create a new incident record by calling the
insertmethod on the incident table with parameters such as impact, urgency, priority, category, user, assignment group, and description. This example uses basic authentication and SOAPpy'sSOAPProxy. - Get Keys: Demonstrates retrieving sysid values of incident records filtered by category, useful for identifying records matching criteria.
- Get Records: Illustrates fetching full incident records filtered by a specified field (e.g., category), enabling retrieval of detailed incident information.
- Get Record by sysid: Shows how to retrieve a specific incident record by providing its unique sysid, enabling precise record access.
- Advanced ECC Queue Integration: Provides a more complex script that monitors a log file for specific keywords, and upon matches, creates ECC queue records in ServiceNow with XML payloads. This example highlights how to automate alert creation based on external system logs using the SOAP API.
Practical Considerations for ServiceNow Customers
- Always use the correct HTTPS endpoint format to avoid connection issues.
- Basic authentication with username and password is used; ensure credentials are properly managed and secured.
- SOAPpy enables direct SOAP API consumption from Python, suitable for legacy SOAP integrations.
- Debugging output can be enabled in the SOAPProxy configuration for troubleshooting SOAP request/response details.
- Advanced examples demonstrate how to extend ServiceNow integration beyond simple CRUD operations to event-driven use cases involving ECC queue processing.
Expected Outcomes
By applying these Python examples, ServiceNow customers can efficiently automate the creation, retrieval, and monitoring of incident records through SOAP web services. Additionally, they can build custom automated workflows that respond to external system events by leveraging the ECC queue, enhancing operational automation capabilities.
Examples demonstrating an integration with a Python web services client.
Requirements
The following examples require the installation of the following Python modules:
insert
This is an example of inserting an
incident.
#!/usr/bin/python
from SOAPpy import SOAPProxy
import sys
def createincident (params_dict ):
# instance to send to
instance = 'demo'
# username/password
username = 'itil'
password = 'itil'
# proxy - NOTE: ALWAYS use https://INSTANCE.service-now.com, not https://www.service-now.com/INSTANCE for web services URL from now on!
proxy = 'https://%s:%s@%s.service-now.com/incident.do?SOAP' % (username , password , instance )
namespace = 'http://www.service-now.com/'
server = SOAPProxy (proxy , namespace )
# uncomment these for LOTS of debugging output #server.config.dumpHeadersIn = 1 #server.config.dumpHeadersOut = 1 #server.config.dumpSOAPOut = 1 #server.config.dumpSOAPIn = 1
response = server. insert (impact = int (params_dict [ 'impact' ] ) , urgency = int (params_dict [ 'urgency' ] ) , priority = int (params_dict [ 'priority' ] ) , category =params_dict [ 'category' ] , location =params_dict [ 'location' ] , caller_id =params_dict [ 'user' ] , assignment_group =params_dict [ 'assignment_group' ] , assigned_to =params_dict [ 'assigned_to' ] , short_description =params_dict [ 'short_description' ] , comments =params_dict [ 'comments' ] )
return response
values = { 'impact': '1' , 'urgency': '1' , 'priority': '1' , 'category': 'High' , 'location': 'San Diego' , 'user': 'fred.luddy@yourcompany.com' , 'assignment_group': 'Technical Support' , 'assigned_to': 'David Loo' , 'short_description': 'An incident created using python, SOAPpy, and web services.' , 'comments': 'This a test making an incident with python.\n Isn \' t life wonderful?' }
new_incident_sysid =createincident (values )
print "Returned sysid: "+ repr (new_incident_sysid )getKeys
This is an example of executing getKeys on the demo instance using basic
authentication.
#!/bin/env python
# use the SOAPpy module from SOAPpy import SOAPProxy
username , password , instance = 'admin' , 'admin' , 'demo'
proxy , namespace = 'https://username:password@www.service-now.com/'+instance+ '/incident.do?SOAP' , 'http://www.service-now.com/'
server = SOAPProxy (proxy ,namespace )
response = server. getKeys (category = 'Network' )
print response. sys_id. split ( ',' )getRecords
In this example, we get an incident, querying for category == "Network" (with basic
authentication).
#!/bin/env python
# use the SOAPpy module from SOAPpy import SOAPProxy
username , password , instance = 'admin' , 'admin' , 'demo'
proxy , namespace = 'https://username:password@www.service-now.com/'+instance+ '/incident.do?SOAP' , 'http://www.service-now.com/'
server = SOAPProxy (proxy ,namespace )
response = server. getRecords (category = 'Network' )
for record in response:
for item in record:
print itemget
In this example, we get an incident record by
sys_id (with basic
authentication).#!/bin/env python
# use the SOAPpy module from SOAPpy import SOAPProxy
username , password , instance = 'admin' , 'admin' , 'demo'
proxy , namespace = 'https://username:password@www.service-now.com/'+instance+ '/incident.do?SOAP' , 'http://www.service-now.com/'
server = SOAPProxy (proxy ,namespace )
response = server. get (sys_id = '9c573169c611228700193229fff72400' )
for each in response:
print eachAdvanced
This is an example of advanced Python script that reads a log file for a keyword
invalid spi and creates an ECC Queue record where the payload is set to an
alert of XML
format.
#!/bin/env python
# kevin.pickard@service-now.com 2008.07.03 initial creation
from SOAPpy import SOAPProxy
from xml. dom. minidom import getDOMImplementation
import sys , os , socket , pickle , re
# instance to send to
instance = 'demo'
# username/pass
username = 'admin'
password = 'admin'
# log file to watch
syslogfile = '/var/log/cisco.log.ksp'
# state file
statefile = '/tmp/syslog_ecc.state-test'
# ECC queue values
soapagent = 'SOAPpy'
ecctopic = 'PIX Error: '
eccname = 'Invalid SPI: '
eccsource = 'Syslog'
# regex string to match
matchstring = 'invalid spi'
try:
state = open (statefile , 'r' )
lastbyte = pickle. load (state )
state. close ( ) except:
lastbyte = 0
#print 'DEBUG: lastbyte = '+str(lastbyte)
try:
log = open (syslogfile , 'ro' ) except:
errortopic = 'Script Error'
errorname = 'Unable to open log file '+syslogfile+ '.'
errorpayload = 'This message was generated due to an error condition encountered in a script. The name of the script is '+ os. path. basename ( sys. argv [ 0 ] )+ ' on server '+ socket. gethostname ( )+ '.'
proxy = 'https://'+username+ ':'+password+ '@'+instance+ '.service-now.com/ecc_queue.do?SOAP'
namespace = 'http://www.service-now.com/'
server = SOAPProxy (proxy , namespace )
server. config. dumpSOAPOut = 1
server. config. dumpSOAPIn = 1
response = server. insert (agent =soapagent , topic =errortopic , name =errorname , source = sys. argv [ 0 ] , payload =errorpayload )
sys. exit ( 1 )
if lastbyte != 0:
try:
log. seek (lastbyte ) except IOError:
pass
loglines =log. readlines ( )
lastbyte =log. tell ( )
log. close ( )
state = open (statefile , 'w' ) pickle. dump (lastbyte , state )
state. close ( )
# regex out the line
matchedlines = [ ] for line in loglines:
if re. search (matchstring , line ) != None:
matchedlines. append (line )
#print 'DEBUG: len->loglines = '+str(len(loglines)) #print 'DEBUG: lastbyte = '+str(lastbyte) #print 'DEBUG: matchedlines = '+str(matchedlines)
if len (matchedlines ) == 0:
sys. exit ( 0 )
proxy = 'https://'+username+ ':'+password+ '@'+instance+ '.service-now.com/ecc_queue.do?SOAP'
namespace = 'http://www.service-now.com/'
server = SOAPProxy (proxy , namespace ) #server.config.dumpSOAPOut = 1 #server.config.dumpSOAPIn = 1
entriestosend = { } for line in matchedlines:
device =line. split ( ) [ 3 ]
sourceip =line. split ( ) [- 1 ]
entriestosend [sourceip ] = [device , line ]
for key ,value in entriestosend. iteritems ( ):
#impl=getDOMImplementation() #newdoc = impl.createDocument(None, "log_line", None) #top_element = newdoc.documentElement #text = newdoc.createTextNode(value[1]) #top_element.appendChild(text)
response = server. insert (agent =soapagent , topic =ecctopic+value [ 0 ] , name =eccname+key , source =eccsource , payload =value [ 1 ] )