Bottom Page

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 !
Quote
#2
Solved myself = closing
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Automated Bet placement redmercury 2 603 Dec-04-2019, 10:53 AM
Last Post: redmercury
  line replacement help mdalireza 8 562 Nov-11-2019, 12:54 PM
Last Post: mdalireza
  variable call back into an array yamifm0f 3 532 Jun-07-2019, 02:44 PM
Last Post: heiner55
  xml replacement with python josesalazmit 3 1,902 Feb-24-2019, 07:28 PM
Last Post: stullis
  Asking for help in solving a single variable nonlinear equation using Python ! NDP 0 453 Feb-15-2019, 12:03 PM
Last Post: NDP
  Call a variable jmf08 16 1,330 Jan-18-2019, 05:50 AM
Last Post: jmf08
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 657 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  call a variable from one function to another ... evilcode1 4 1,277 Sep-16-2018, 10:50 AM
Last Post: gruntfutuk
  How to call a variable declared in a function in another python script lravikumarvsp 2 1,130 Jun-04-2018, 08:45 AM
Last Post: Larz60+
  Is pathlib a viable replacement for os.path? j.crater 4 5,738 Jan-13-2018, 09:49 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)