Search code examples
pythonwhois

The whois.whois function always gets a timed out error


The whois.whois function always gets a timed out error. At first, I thought it was because my project is written in Python 2.7 but I also checked in 3.7 and got the same error. I checked the address on the online website that uses whois and the link worked and didn't get this error. Anyone knows why this is happening?

import whois

w = whois.whois("https://stackoverflow.com")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python37\lib\site-packages\whois\__init__.py", line 43, in whois
    text = nic_client.whois_lookup(None, domain.encode('idna'), flags)
  File "C:\Program Files\Python37\lib\site-packages\whois\whois.py", line 264, in whois_lookup
    result = self.whois(query_arg, nichost, flags)
  File "C:\Program Files\Python37\lib\site-packages\whois\whois.py", line 142, in whois
    s.connect((hostname, 43))
socket.timeout: timed out


Solution

  • Your code has at least two problems, and you may have a network problem also.

    However, there is no reason for it not to work on Python2.

    About the code

    This works perfectly fine:

    In [7]: import whois
    
    In [8]: print whois.query('stackoverflow.com').expiration_date
    2020-02-02 11:59:59
    

    Note two things:

    • whois is about domain names, not URLs; so you should pass a domain name; note more generally that for new endeavors you should have a look at RDAP instead of whois since you will get a far better experience
    • you need to use whois.query not whois.whois (you are not saying which version of the library you use, but at its documentation page on https://pypi.org/project/whois/ you can clearly see it is whois.query so I do not know where your whois.whois comes from).

    About the network

    You show a network error. It is not 100% clear but you may or may not have access to the whois servers you want to query.

    Easy way to test: just use the command line whois from the same box as your code (but again use a domain name, not a URL as parameter) and you will see what is happening.

    You can even do directly a telnet on port 43 as whois does nothing else.

    $ echo 'stackoverflow.com' | nc whois.verisign-grs.com 43 | grep 'Expiry'
       Registry Expiry Date: 2020-02-02T11:59:59Z