将subprocess.call数据存储到可变数据库中

时间:2016-05-20 16:09:57

标签: python python-2.7

您正在编写代码以获取网站列表的卷曲响应

这是我的尝试

import requests
import urllib
import urllib2
import os
import subprocess
f = open("subdomains.txt")
line =f.readlines()
i=0;
while(i<100):
    x=(line[i]).rstrip("\n")
    def convert(url):
        if url.startswith('http://www.'):
            return 'http://' + url[len('http://www.'):]
        if url.startswith('www.'):
            return 'http://' + url[len('www.'):]
        if not url.startswith('http://'):
            return 'http://' + url
        return url
    print convert(x)
    res = subprocess.call("curl -I " +convert(x),shell=True)
    i=i+1
    print "______________________"
    w=open("output.txt".'a')
    w.write(res)
    w.close

如果我删除最后3行。代码有效工作,但将数据附加到输出文件的部分失败。任何人都可以建议我如何将subprocess.call数据附加到输出文件中?运行我的代码后得到的错误是

Traceback (most recent call last):
  File "shashank01.py", line 24, in <module>
    w.write(res)
TypeError: expected a string or other character buffer object

1 个答案:

答案 0 :(得分:1)

f = open("output.txt",'a')
subprocess.call("curl -I " +convert(x),shell=True,stdout=f)