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

Python 

If website takes too long time to load

Hi.

I'm making a program that reads a specific site and then it writes the whole site to a .txt file.
I want to make it so if the website takes more than 1 second to load it skips and goes to the next site.

My code looks something like this:

Code
import urllib.request
Number = 123
url = "http:/website.com/number={}"

while True:
    read = urllib.request.urlopen(url.format(x)).read()
    
    text_file = open("text.txt", "ab")
    text_file.write(read)
    text_file.close()
    print(read)
    Number = Number + 1
Something like this should work.
http://docs.python-requests.org/en/maste.../#timeouts

Code
import requests

while True:
    #set timeout in seconds
    try:
        requests.get('https://website.com', timeout=0.05)
    except requests.exceptions.Timeout as e:
        print("Website timed out")
[Image: 0DvFxnA.gif]
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn
Thanks works perfect! :)

Sent u some bytes


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by McTorturoFeb 15, 2022 11:01 AM 29 725 Feb 15, 2022 11:01 AM
Last Post: QProxies
[C] by HiddenEyEAug 6, 2020 01:11 AM 19 593 Aug 6, 2020 01:11 AM
Last Post: HRRHHRRH
[C] by FaberCastellSep 10, 2017 07:28 AM 12 434 Sep 10, 2017 07:28 AM
Last Post: Ronnie Coleman