如何为python的mechanize设置超时值?

时间:2022-12-29 15:50:21

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

如何为python的mechanize设置超时值?

3 个解决方案

#1


13  

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).

亚历克斯是正确的:机械化。urlopen接受一个超时参数。因此,只需在浮点数中插入数秒:mechanize。urlopen(http://url/,超时= 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.

什么是mechanize._sockettimeout。_GLOBAL_DEFAULT_TIMEOUT你问?它只是套接字模块的设置。

import socket

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

#2


3  

If you're using Python 2.6 or better, and a correspondingly updated version of mechanize, mechanize.urlopen should accept a timeout=... optional argument which seems to be what you're looking for.

如果您使用的是Python 2.6或更高版本,以及相应更新的mechanize, mechanize。urlopen应该接受一个超时=…可选的论点,似乎是你要找的。

#3


1  

I believe something along the lines of

我相信一些类似的东西

mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = 100

will override the default value Mechanize uses.

将覆盖Mechanize使用的默认值。

#1


13  

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).

亚历克斯是正确的:机械化。urlopen接受一个超时参数。因此,只需在浮点数中插入数秒:mechanize。urlopen(http://url/,超时= 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.

什么是mechanize._sockettimeout。_GLOBAL_DEFAULT_TIMEOUT你问?它只是套接字模块的设置。

import socket

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

#2


3  

If you're using Python 2.6 or better, and a correspondingly updated version of mechanize, mechanize.urlopen should accept a timeout=... optional argument which seems to be what you're looking for.

如果您使用的是Python 2.6或更高版本,以及相应更新的mechanize, mechanize。urlopen应该接受一个超时=…可选的论点,似乎是你要找的。

#3


1  

I believe something along the lines of

我相信一些类似的东西

mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT = 100

will override the default value Mechanize uses.

将覆盖Mechanize使用的默认值。