Python Forum
Simple automated SoapAPI Call with single variable replacement from csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple automated SoapAPI Call with single variable replacement from csv
#1
Good Afternoon Gents/Ladies

I am not a natural coder but have been attempting to solve an automation issue with Python, the code I have hacked together below simply pulls a variable (serial number) from a CSV and pipes it into a rather simple SoapAPI call, the idea idea being that it cycles through a few thousand serial numbers in the CSV until completion/endOFserials

Summary
1) take serial from CSV inject variable into Soap Serial field <ns2:serialNumber>{1}</ns2:serialNumber>
2) Run SoapAPI call --> end session
3) take next serial Run SoapAPI call --> end session
4) and so on ~

I am attempting to code this with Python 2.7 along with Python moduel "requests"

I have successfully managed to pull the first serial number but have run into 3 error which I am hoping you can point me in the right direction.

#!/usr/bin/env python import requests import csv import getopt import sys def read_run(url, csv_file): csv_reader = csv.reader(open(csv_file)) for row in csv_reader: print row if len(row) == 1: serial_number = row[0] associate_element(url, serial_number) else: print 'Invalid csv file provided' print 'Only support below format' print 'serial_number' sys.exit(0) print 'Complelte.' def associate_element(url, serial_number): request = u"""<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:Username>NBIUSER</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">PWhere</wsse:Password> </wsse:UsernameToken> </wsse:Security> </soap:Header> <soap:Body> <ClearDeviceRecordRequest xmlns="http://twowire.com/dmc/apps/nbiws/devicemgmt/v1_0" xmlns:ns2="http://twowire.com/dmc/apps/nbiws/base/v1_0"> <deviceSelector xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SerialNumberAndOUIDeviceSelector"> <ns2:serialNumber>{1}</ns2:serialNumber> <ns2:oui>446AB7</ns2:oui> </deviceSelector> </ClearDeviceRecordRequest> </soap:Body> </soap:Envelope>""".format(serial_number) soap_request(url, request) def soap_request(url, request): encoded_request = request.encode('utf-8') headers = {'Content-Type': 'text/xml'} response = requests.post(url=url, headers=headers, data=encoded_request, verify=False) #print response.content response.content if __name__ == '__main__': opts, args = getopt.getopt(sys.argv[1:], 'hu:f:', ['url=', 'file=', 'help']) sample_file = 'sample.csv' soap_url = 'http://poc5.server.online/dmc-nbiws/' for k, v in opts: if k in ['-h', '--help']: print 'Usage :' print '-h or --help\t Display script usage' print '-u or --url\t The URL of Soap request, if it is not provided, using szeco142 by default' print """-f or --file\t Sample data file, must be a csv file, if it is not provided, using /opt/sample.csv by default""" sys.exit(0) if k in ['-u', '--url']: soap_url = v if k in ['-f', '--file']: sample_file = v print soap_url print sample_file read_run(soap_url, sample_file)
Here's the output from running the script

./soap_provision.py -u http://poc5.server.online/dmc-nbiws/ -f sample2.csv

Error:
http://poc5.server.online/dmc-nbiws/ sample2.csv ['M91902SC09BK'] Traceback (most recent call last): File "./soap_provision.py", line 76, in <module> read_run(soap_url, sample_file) File "./soap_provision.py", line 15, in read_run associate_element(url, serial_number) File "./soap_provision.py", line 42, in associate_element </soap:Envelope>""".format(serial_number) IndexError: tuple index out of range
Hopefully, you'll be able to see my school-boy/Newbie issues

Many Thanks !
Reply
#2
Solved myself = closing
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Use generator with sring replacement... [solved] SpongeB0B 9 1,076 Oct-10-2025, 06:43 AM
Last Post: Pedroski55
  not able to call the variable inside the if/elif function mareeswaran 3 1,497 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  If I need to do regex replacement 27000 times, how to shorten it? tatahuft 4 1,490 Dec-26-2024, 01:51 AM
Last Post: deanhystad
  Variable Substitution call keys Bobbee 15 4,551 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  replacement for fpformat.extract GbSig1998 4 1,947 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 9,536 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  String replacement in DB WJSwan 0 1,607 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 16,613 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Delete multiple comments with a single API call (facebook) Ascalon 0 3,939 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Simple Variable Saving in Loop DevDev 3 4,899 Mar-09-2021, 07:17 PM
Last Post: Fre3k

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.