Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用pycurl时限制python中reponse.getvalue()的输出_Python_Python 2.7_Python 3.x_Pycurl - Fatal编程技术网

如何在使用pycurl时限制python中reponse.getvalue()的输出

如何在使用pycurl时限制python中reponse.getvalue()的输出,python,python-2.7,python-3.x,pycurl,Python,Python 2.7,Python 3.x,Pycurl,我使用python脚本在服务器列表上执行curl命令,并将输出写入各个服务器。下面是配置文件 server1、server2、server3 编写python脚本是为了实现我的脚本一次读取一个服务器名称,并将该服务器的输出写入单个文件。与此类似,如果三个服务器名称,则表示每个文件中有三个文件,每个文件中有各自的输出。但实际上,当脚本执行时,它会将输出写入第一个文件。当读取第二个服务器名时,它将第一个服务器和第二个服务器的输出写入第二个文件,以此类推。哪一个理想应该没有人能告诉我在下面的脚本中我做

我使用python脚本在服务器列表上执行curl命令,并将输出写入各个服务器。下面是配置文件

server1、server2、server3

编写python脚本是为了实现我的脚本一次读取一个服务器名称,并将该服务器的输出写入单个文件。与此类似,如果三个服务器名称,则表示每个文件中有三个文件,每个文件中有各自的输出。但实际上,当脚本执行时,它会将输出写入第一个文件。当读取第二个服务器名时,它将第一个服务器和第二个服务器的输出写入第二个文件,以此类推。哪一个理想应该没有人能告诉我在下面的脚本中我做错了什么

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()
导入操作系统
导入pycurl
导入StringIO
导入文件输入
导入系统
response=StringIO.StringIO()
opfilename=“output.log”
configfile=“config.properties”
文件=打开(配置文件,'r')
计数器=0
长度=0
打开(configfile,'r')作为configfile:
对于configfile中的项目:
行=items.strip()
值=行分割(“,”)
长度=长度(值)
当计数器<长度:
打印计数器
服务器名=值[计数器]
打印服务器名
c=pycurl.Curl()
c、 setopt(c.URL,'http://'+servername+':port/solr/admin/cores?action=STATUS')
c、 setopt(c.WRITEFUNCTION,response.write)
c、 执行
f=打开(服务器名+'output.log','w')
打印>>f,response.getvalue()+'\r'+'\n'
计数器=计数器+1
c、 关闭()
答复:close()

.getvalue()
不会清除StringIO的缓冲区-每次通过循环时都需要关闭并重新打开StringIO。然而,我认为您根本没有理由使用StringIO;您也可以轻松地让Curl对象直接写入磁盘文件。感谢@jasonharper它起作用,我将脚本更改为
c.setopt(c.WRITEFUNCTION,f.write)
以写入磁盘,而不是StringIO