Jun-30-2020, 04:22 PM
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.
./soap_provision.py -u http://poc5.server.online/dmc-nbiws/ -f sample2.csv
Many Thanks !
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 issuesMany Thanks !