3

Estoy utilizando la librería requests en python (2.7), preciso configurar la petición estableciendo tiempo de espera de conexión y de lectura, y estoy usando un ejemplo del sitio del proyecto python-requests.org, pero lanza un error: "timeout on request" aun y cuando estoy intentando ir contra google. Alguna idea?

import requests
r = requests.get('https://google.com', timeout=(20, 60))

Estoy usando la versión de la librería 1.0.2

9
  • Mira esta guia para que tu pregunta sea mejor recibida. Tambien, aprovecha y haz el tour para entender mejor como funcionamos y de paso obtener tu primera medalla!
    – gbianchi
    el 11 ago. 2017 a las 20:07
  • Hola, al parecer no existe esa versión de requests, ¿estás seguro que es la 1.11 y no la 0.11 o 1.1?
    – César
    el 11 ago. 2017 a las 20:26
  • Si, es la versión 1.11.6 el 11 ago. 2017 a las 20:52
  • @perodriguezl intento instalar esa versión y me arroja: Could not find a version that satisfies the requirement requests==1.11.6
    – César
    el 11 ago. 2017 a las 20:54
  • usa asi el atributo timeout requests.get('http://google.com', timeout=0.5) el 11 ago. 2017 a las 20:57

1 respuesta 1

2

El problema es que para tu versión, no estaba implementada esa característica. Observa el código de esa versión:

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.
    Returns :class:`Response <Response>` object.
    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
    :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
    :param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) Float describing the timeout of the request.
    :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    """

    session = sessions.Session()
    return session.request(method=method, url=url, **kwargs)

A diferencia de la versión actual con respecto a ese parámero (usando una tupla):

:param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.

Por lo tanto, me parece que tienes solo dos opciones:

  1. Usar simplemente timeout con un dato de tipo float
  2. Actualizar a una versión reciente que soporte dicho comportamiento

    Si estás usando pip puedes hacer:

    $ pip install --upgrade requests
    
1
  • Excelente, muy acertado, se me paso verificar eso, lamentablemente no puedo hacer upgrade a ese paquete ya que en el server de prod esa es la version que esta. el 11 ago. 2017 a las 21:13

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.