Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i set a timeout value for python's mechanize?

How do i set a timeout value for python's mechanize?

like image 662
Joe Schmoe Avatar asked Aug 24 '10 01:08

Joe Schmoe


1 Answers

Alex is correct: mechanize.urlopen takes a timeout argument. Therefore, just insert a number of seconds in floating point: mechanize.urlopen('http://url/', timeout=30.0).

The background, from the source of mechanize.urlopen:

def urlopen(url, data=None, timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
    ...
    return _opener.open(url, data, timeout)

What is mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT you ask? It's just the socket module's setting.

import socket

try:
    _GLOBAL_DEFAULT_TIMEOUT = socket._GLOBAL_DEFAULT_TIMEOUT
except AttributeError:
    _GLOBAL_DEFAULT_TIMEOUT = object()
like image 57
Tim McNamara Avatar answered Oct 25 '22 06:10

Tim McNamara