Welcome, guest | Sign In | My Account | Store | Cart

This recipe will open a certain URL approximately every 0.5 second using your default browser. It continues to do so until a timeout occurs which has been specified (like the URL) by the user in number of minutes. The timeout starts a reminder that ends once the program has received input. The program then pauses until a key is struck or exits if ESC is pressed.

Python, 41 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import msvcrt
import time
import webbrowser
import winsound

def main():
    url = raw_input('URL = ')
    while True:
        try:
            timeout = float(raw_input('Timeout = ')) * 60
            if timeout > 0:
                break
            print 'Timeout must be positive.'
        except:
            print 'Timeout must be a number.'
    while True:
        print 'Executing ...'
        start = time.clock()
        while time.clock() - start < timeout:
            webbrowser.open(url, False, False)
            time.sleep(0.5)
        getch_all(False)
        while not msvcrt.kbhit():
            winsound.Beep(1000, 500)
            time.sleep(0.5)
        getch_all(False)
        print 'Pausing ...'
        if getch_all(True) == '\x1b':
            break

def getch_all(block):
    if block:
        buf = msvcrt.getch()
    else:
        buf = str()
    while msvcrt.kbhit():
        buf += msvcrt.getch()
    return buf

if __name__ == '__main__':
    main()

This program was designed to run on Microsoft Windows -- hence the imports of "msvcrt" and "winsound." This program is useful for refreshing a web page using a real internet browser (for whatever reason). Suggested use is for providing a simulated load on a web server for testing purposes and would probably be most useful if run simultaneously on several computers while pointed to one location. This program is simple and should provide an example of how to do lite manipulation of web browsers, timing, and interaction with a computer running Windows.