Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Check if a site exists within a second

Checking if a website exists it easy, but if it doesn't exist it takes ages to verify.

E.g

Code
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request("http://stackoverflow.com")
try:
   response = urlopen(req)
except HTTPError as e:
   print('The server couldn\'t fulfill the request.')
   print('Error code: ', e.code)
except URLError as e:
   print('We failed to reach a server.')
   print('Reason: ', e.reason)
else:
   print ('Website is working fine')

If the site doesn't exist, it takes ages to verify it. I need to it check within 2 seconds. Any ideas?
I just tried your script, and when entering a random website I receive this response within a second:

We failed to reach a server.
Reason: [Errno 11001] getaddrinfo failed

Which reason are you getting when using a random non-existing website?
use urlopen('url', timeout=2)

i've tested this with a long polling request. it give's a timeout. website exist

Code
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
from socket import timeout


def main():
    req = Request("http://domain.net/test/sleep.php")
    try:
        response = urlopen(req, timeout=2)
    except HTTPError as e:
        print('The server couldn\'t fulfill the request.')
        print('Error code: ', e.code)
    except URLError as e:
        print('We failed to reach a server.')
        print('Reason: ', e.reason)
    except timeout as e:
        print('Socket timeout.')
    else:
        print('Website is working fine')


if __name__ == '__main__':
    main()
you could use head request instead (e.g. by using requests library)

Quote:The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

(Apr 12, 2018 12:33 PM).kr. Wrote:  use urlopen('url', timeout=2)

i've tested this with a long polling request. it give's a timeout. website exist

Code
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
from socket import timeout


def main():
   req = Request("")
   try:
       response = urlopen(req, timeout=2)
   except HTTPError as e:
       print('The server couldn\'t fulfill the request.')
       print('Error code: ', e.code)
   except URLError as e:
       print('We failed to reach a server.')
       print('Reason: ', e.reason)
   except timeout as e:
       print('Socket timeout.')
   else:
       print('Website is working fine')


if __name__ == '__main__':
   main()

same i did the same, idk


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by gigajewJun 20, 2019 01:18 AM 9 390 Jun 20, 2019 01:18 AM
Last Post: henkhamerman
[.NET] by Formula 1Mar 1, 2019 09:19 PM 3 311 Mar 1, 2019 09:19 PM
Last Post: 0xDEAD10CC
[Java] by ShxtdownFeb 7, 2019 08:20 PM 7 300 Feb 7, 2019 08:20 PM
Last Post: Caliban