mboot-github / python-whois

Python module/library for retrieving domain WHOIS information (only domain)
https://pypi.org/project/whois/
MIT License
289 stars 136 forks source link

I think a try-except would be better when trying more details. #304

Closed baigreen closed 1 year ago

baigreen commented 1 year ago
>>> whois.query("web3gpt.com")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/code/whois/python_whois/whois/__init__.py", line 279, in query
    whois_str = do_query(
  File "/home/ubuntu/code/whois/python_whois/whois/_1_query.py", line 71, in do_query
    _do_whois_query(
  File "/home/ubuntu/code/whois/python_whois/whois/_1_query.py", line 194, in _do_whois_query
    raise WhoisCommandFailed(r)
whois.exceptions.WhoisCommandFailed: fgets: Connection reset by peer
   Domain Name: WEB3GPT.COM
maarten-boot commented 1 year ago

these issue change depending on your geolocation, in fact this domain produces no real output at all for me (after the redirect to china)

python-whois]$ python
Python 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import whois
>>> d = whois.query("web3gpt.com")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mboot/DEV/00-github.com/mboot/python-whois/whois/__init__.py", line 297, in query
    data = do_parse(
           ^^^^^^^^^
  File "/home/mboot/DEV/00-github.com/mboot/python-whois/whois/_2_parse.py", line 260, in do_parse
    return handleShortResponse(tld, dl, whois_str, verbose)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mboot/DEV/00-github.com/mboot/python-whois/whois/_2_parse.py", line 163, in handleShortResponse
    raise FailedParsingWhoisOutput(whois_str)
whois.exceptions.FailedParsingWhoisOutput: [Querying whois.verisign-grs.com]
[Redirected to whois.22.cn]
[Querying whois.22.cn]
[whois.22.cn]
maarten-boot commented 1 year ago

we actually use a try: block

https://github.com/DannyCork/python-whois/blob/master/whois/_1_query.py#L182

maarten-boot commented 1 year ago

what would you prefer in this case, a simple None response without any cause or should we add something like the whois.query() returns None and a additional call like last_status could return:

FailedParsingWhoisOutput (in my case) or WhoisCommandFailed (in your case)

in my test2.py program i use:

        try:
            testItem(d)
        except whois.UnknownTld as e:
            errorItem(d, e, what="UnknownTld")
        except whois.FailedParsingWhoisOutput as e:
            errorItem(d, e, what="FailedParsingWhoisOutput")
        except whois.UnknownDateFormat as e:
            errorItem(d, e, what="UnknownDateFormat")
        except whois.WhoisCommandFailed as e:
            errorItem(d, e, what="WhoisCommandFailed")
        except whois.WhoisQuotaExceeded as e:
            errorItem(d, e, what="WhoisQuotaExceeded")
        except whois.WhoisPrivateRegistry as e:
            errorItem(d, e, what="WhoisPrivateRegistry")
        except whois.WhoisCommandTimeout as e:
            errorItem(d, e, what="WhoisCommandTimeout")
baigreen commented 1 year ago

I am not sure. In my opinion, the service for getting basic information should be stable.

For .com, I think the first step is whois -h whois.verisign-grs.com web3gpt.com to get the basic information, and the second step is whois -h whois.22.cn web3gpt.com Even if the second step fails, it should return the main information already obtained in the first step.

maarten-boot commented 1 year ago

Well that happens all inside the cli whois command and is different on mac then on linux. on Mac the cli whois has a tendency to show all steps of the redirect, on Linux the cli whois only shows the final redirect result.

I have no influence on the behavior of how the cli whois does its work, all we can do is ignore the error code of whois and do our best to parse the result if there is any, for the ignore error code we already have a option on whois.query

example with your command above (on linux fedora 37):

whois -h whois.verisign-grs.com web3gpt.com
[Querying whois.verisign-grs.com]
[Redirected to whois.22.cn]
[Querying whois.22.cn]
[whois.22.cn]
 whois -h whois.22.cn web3gpt.com
[Querying whois.22.cn]
[whois.22.cn]
baigreen commented 1 year ago

306 add a simple way to ignore the error.

Will close it.