简体   繁体   English

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

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

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

Alex is correct: mechanize.urlopen takes a timeout argument. Alex是正确的: mechanize.urlopen采用timeout参数。 Therefore, just insert a number of seconds in floating point : mechanize.urlopen('http://url/', timeout=30.0) . 因此,只需在浮点数插入一些mechanize.urlopen('http://url/', timeout=30.0)

The background, from the source of mechanize.urlopen : 来自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? 你问的是什么是mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT It's just the socket module's setting. 这只是socket模块的设置。

import socket

try:
    _GLOBAL_DEFAULT_TIMEOUT = socket._GLOBAL_DEFAULT_TIMEOUT
except AttributeError:
    _GLOBAL_DEFAULT_TIMEOUT = object()

如果你正在使用Python 2.6或更高版本,以及相应更新的mechanize版本, mechanize.urlopen应该接受一个timeout=...可选参数,这似乎是你正在寻找的。

I believe something along the lines of 我相信一些事情

mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = 100

will override the default value Mechanize uses. 将覆盖Mechanize使用的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM