Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Simple automated SoapAPI Call with single variable replacement from csv
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

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)
            print 'Invalid csv file provided'
            print 'Only support below format'
            print 'serial_number'
    print 'Complelte.'

def associate_element(url, serial_number):
    request = u"""<soap:Envelope xmlns:soap="">
      <wsse:Security xmlns:wsse="">
         <wsse:UsernameToken xmlns:wsu="">
            <wsse:Password Type="">PWhere</wsse:Password>
      <ClearDeviceRecordRequest xmlns="" xmlns:ns2="">
         <deviceSelector xmlns:xsi="" xsi:type="ns2:SerialNumberAndOUIDeviceSelector">
    soap_request(url, request)

def soap_request(url, request):
    encoded_request = request.encode('utf-8')
    headers = {'Content-Type': 'text/xml'}
    response =, headers=headers, data=encoded_request, verify=False)
    #print response.content

if __name__ == '__main__':
    opts, args = getopt.getopt(sys.argv[1:], 'hu:f:', ['url=', 'file=', 'help'])

    sample_file = 'sample.csv'
    soap_url = ''
    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"""

        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

./ -u -f sample2.csv

Error: sample2.csv ['M91902SC09BK'] Traceback (most recent call last): File "./", line 76, in <module> read_run(soap_url, sample_file) File "./", line 15, in read_run associate_element(url, serial_number) File "./", 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 !
Solved myself = closing

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)