shashank
shashank

Reputation: 19

Storing subprocess.call data to a varaible

Hi am writing a code to get the curl response of a list of website

Here is my attempt

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

If I remove the last 3 lines . The code works efficiently but the part to append the data to the output file fails. Anyone can suggest me how to make the subprocess.call data append into the output file ? The error what I get get after running my code is

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

Upvotes: 0

Views: 31

Answers (1)

Anoop
Anoop

Reputation: 2798

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

Upvotes: 1

Related Questions