Python拆分问题

时间:2015-08-17 11:10:57

标签: python

我试图从文件中获取域列表的SPF记录。文件将包含输入,如

Input_Domains.txt

1,box.com
2,blah.com
3,....

脚本:

#!/usr/bin/python

    import sys
    import socket
    import dns.resolver
    import re


    def getspf (domain):
       answers = dns.resolver.query(domain, 'TXT')
       for rdata in answers:
         for txt_string in rdata.strings:
           if txt_string.startswith('v=spf1'):
             return txt_string.replace('v=spf1','')

    with open('Input_Domains.txt','r') as f:
         full_spf=""
         spf_rec=""
         for line in f:
            x=line.split(',')
            app_id=x[0]
            domains=x[1]
            try:

               full_spf+=getspf(domains.strip())+"\n"
               #output=full_spf
               #o_spf=x[0]+","+full_spf+=getspf(x.strip())+"\n"
            except Exception:
                 pass
    my_file=open("out_spf.txt","w")
    my_file.write(full_spf)
    my_file.close()

现在,我得到的输出只是特定域的记录。我想得到App_ID后跟域名的spf记录。有什么建议吗?

Example Output :
1,SPF records of box.com...
2,spf recors of blah.com....
......

1 个答案:

答案 0 :(得分:3)

你的代码问题在于,当你应该为每一行写作时,你只写了一次。

with open('Input_Domains.txt','r') as f:
    with open('Output_spf.txt', 'w') as g:
        for line in f:
            # ...
            g.write(full_spf)