-2

First, I run nslookup -q=TXT _netblocks.google.com 8.8.8.8 in bash, and get the result like this:

  Server:   8.8.8.8
  Address:  8.8.8.8#53

  Non-authoritative answer:
  _netblocks.google.com text = "v=spf1 ip4:64.18.0.0/20 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:207.126.144.0/20 ip4:209.85.128.0/17 ip4:216.58.192.0/19 ip4:216.239.32.0/19 ~all"

  Authoritative answers can be found from:

Now, I have a task to get the same result in python, and can't use os lib to run bash command.
Which lib can I use? And how to use?

He Yuntao
  • 86
  • 11
  • http://stackoverflow.com/questions/1189253/how-do-i-resolve-an-srv-record-in-python explains how to do this in native Python (though with SRV instead of TXT). – tripleee Nov 04 '15 at 10:35
  • @tripleee Thank you. That solved my problem. But why I was voted down... – He Yuntao Nov 04 '15 at 11:00
  • I'm not the downvoter, but if this is a duplicate, please mark it as such. – tripleee Nov 04 '15 at 11:38

1 Answers1

0

Although you should have posted what you've already tried (as specified in the MCVE page), here's a sample that uses subprocess:

import subprocess
import shlex

command = "nslookup -q=TXT _netblocks.google.com 8.8.8.8"
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
# if running on Windows, split for ("\r\n")
for line in out.split("\n"):
    # process the line, in our case simply print it
    print(line)
Community
  • 1
  • 1
CristiFati
  • 38,250
  • 9
  • 50
  • 87