开发者

How do I use python to hit this command and return the result?

$whois abc.com

I want to use python to hit this comman开发者_开发知识库d, and then give the result as a String of text. How can I do that?


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).


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.


With subprocess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜