views:

92

answers:

3
$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?

A: 

With subprocess.

Ignacio Vazquez-Abrams
+2  A: 

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
When I say "print output" , it returns None?
TIMEX
That shouldn't happen. What does whois print when you execute the command in the shell?
AndiDog
Oh, as of the documentation you actually need the `stdout = PIPE` parameter to get a result other than `None`.
AndiDog
A: 

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