2

これの前に「もう1人の初心者」というテキストを付けます。Popenコマンドを介してwhoisコマンドの結果が得られた場合、それが適切かどうかをどのようにテストしますか?

通常、Pythonがその長さをテストできるもののリストを返す場合、通常はそれで十分ですが、これはもう少し恣意的です。

たとえば、ドメインの原産国をテストしていますが、gethostbyaddrが提供するドメインがWHOISサーバーによって認識されない場合があります。だから、失敗した場合はIPを送信するつもりだったのですが、70文字以上のテストで終わりました。誰かがこれを行うための「標準的な」方法が何であるかを知っているかどうか疑問に思っています。

w = Popen(['whois', domain], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                whois_result = w.communicate()[0]
                print len(whois_result)
                if len(whois_result) <= 70:
                        w = Popen(['whois', p_ip], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                        whois_result = w.communicate()[0]
                        print len(whois_result)
                        if len(whois_result) <= 70:
                                print "complete and utter whois failure, its you isnt it, not me."
                        test = re.search("country.+([A-Z].)",whois_result)
                        countryid = test.group(1)
4

1 に答える 1

1

直接の質問に答えるには、コマンドの出力でこの文字列を探してwhois、問題があったかどうかを確認してください...

「insert_domain_here」に一致しません

あなたのタスクに他の意味のある問題に対処するために...あなたのPopenコマンドは困難な方法で物事を進めています...あなたはPIPEforを必要とせず、これをもう少し効率的にするために直接stdin呼び出すことができます...私は書き直しましたあなたが心に留めていると思うこと....communicate()Popen

from subprocess import Popen, PIPE, STDOUT
import re

## Text result of the whois is stored in whois_result...
whois_result = Popen(['whois', domain], stdout=PIPE,
    stderr=STDOUT).communicate()[0]
if 'No match for' in whois_result:
    print "Processing whois failure on '%s'" % domain
    whois_result = Popen(['whois', p_ip], stdout=PIPE,
        stderr=STDOUT).communicate()[0]
    if 'No match for' in whois_result:
            print "complete and utter whois failure, its you isnt it, not me."
    test = re.search("country.+([A-Z].)",whois_result)
    countryid = test.group(1)
于 2011-11-27T23:21:35.037 に答える