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
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,018 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  Question about Creating an Automated Process in Python Supratik1234 0 712 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Need help on how to include single quotes on data of variable string hani_hms 5 1,888 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  String replacement in DB WJSwan 0 713 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,972 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,268 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Simple Variable Saving in Loop DevDev 3 2,960 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  Creating symbolic matrix automated NMMST 2 2,061 Oct-05-2020, 01:42 AM
Last Post: scidam
  Automated service status for windows Pkhan 2 2,091 Sep-30-2020, 10:30 AM
Last Post: Pkhan
  Extract the largest value from a group without replacement (beginner) preliator 1 2,039 Aug-12-2020, 01:56 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020