2
$whois abc.com

I want to use python to hit this command, and then give the result as a String of text. How can I do that?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    Duplicate: http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python – S.Lott Jun 14 '10 at 21:37
  • 1
    Not really a duplicate since the question you mention is a GENERIC one while this one is quite specific and there are better ways to do whois than to call the external command. – bortzmeyer Jun 16 '10 at 06:48

3 Answers3

4

You can use subprocess, for example:

from subprocess import Popen, PIPE
output = Popen(["/usr/bin/whois", "abc.com"], stdout = PIPE).communicate()[0]

The stdout = PIPE parameter forces stdout to be written to a temporary pipe instead of the console (if you don't want that, remove the stdout parameter).

AndiDog
  • 68,631
  • 21
  • 159
  • 205
1

subprocess is fine. On the other hand, the whois protocol is so simple that I do not see why to use an external command (and depend on its availability). Just open a TCP connection to port 43, send a one-line query and read the responses.

bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
  • most other whois servers or api out there return junk for IPs but Linux whois is the only one that somehow chases the ip till it gets the actual whois off the final node. I am not sure if I am making myself clear, just wanted to explain the authors problem as I face it too. – Max May 18 '11 at 22:41
0

With subprocess.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358