0

Am using python script to execute a curl command on a list of servers and write the output to individual servers. below is the config file

server1, server2, server3

And the python script is written in to achieve that my script should read one server name at a time and write the output of that server into a individual file. like this if three server name, means three files with respective output in each file. But in actual when the script executes it writes the output to first file. When it read second server name it writes the output of first server and second server to second file and so on . which ideal should not could any one tell me what am doing wrong in the below script

import os
import pycurl
import StringIO
import fileinput
import sys

response=StringIO.StringIO()
opfilename="output.log"
configfile="config.properties"
file=open(configfile,'r')
counter=0
length=0
with open(configfile,'r') as configfile:        
    for items in configfile: 
            line=items.strip()  
            values=line.split(",") 
            length=len(values) 
            while counter < length: 
                    print counter
                    servername=values[counter]
                    print servername
                    c=pycurl.Curl()
                    c.setopt (c.URL, 'http://'+servername+':port/solr/admin/cores?action=STATUS')
                    c.setopt (c.WRITEFUNCTION, response.write)
                    c.perform()
                    f=open(servername+'output.log','w')
                    print >> f,  response.getvalue()  + '\r' + '\n'
                    counter=counter+1
                    c.close()
            response.close()
Adarsh H D Dev
  • 588
  • 7
  • 29
  • 1
    ``.getvalue()`` does not clear the StringIO's buffer - you would need to close and reopen the StringIO each time through the loop. However, I see no good reason for you to be using a StringIO at all; you could just as easily have the Curl object write directly to your disk file. – jasonharper Jan 23 '17 at 16:57
  • Thanks @jasonharper it worked i changed the script as below ```c.setopt (c.WRITEFUNCTION, f.write)``` to write to disk instead of StringIO – Adarsh H D Dev Jan 23 '17 at 17:17

0 Answers0