如何为python的机械化设置超时值?

Joe*_*moe 15 python timeout mechanize

如何为python的机械化设置超时值?

Tim*_*ara 13

亚历克斯是正确的:mechanize.urlopen接受timeout争论.因此,只需在浮点数中插入若干:mechanize.urlopen('http://url/', timeout=30.0).

背景,来源mechanize.urlopen:

def urlopen(url, data=None, timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
    ...
    return _opener.open(url, data, timeout)
Run Code Online (Sandbox Code Playgroud)

什么是mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT你问?这只是socket模块的设置.

import socket

try:
    _GLOBAL_DEFAULT_TIMEOUT = socket._GLOBAL_DEFAULT_TIMEOUT
except AttributeError:
    _GLOBAL_DEFAULT_TIMEOUT = object()
Run Code Online (Sandbox Code Playgroud)