How to declare a timeout using urllib2 in Google App Engine?

I know that it urllib2is available in the Google App Engine as an Urlfetch wrapper, and, as you know, Universal Feedparser uses urllib2.

Do you know any method to set a timeout on urllib2?
Is the option timeoutenabled on urllib2 in the version of the Google App Engine application?

I'm not interested in a method like:

rssurldata = urlfetch(rssurl, deadline=..)
feedparser.parse(rssurldata)
+5
source share
4 answers

There is no easy way to do this, since the wrapper does not provide a way to pass a timeout value to my knowledge. One of the hacking options would be to disable the urlfetch API:

old_fetch = urlfetch.fetch
def new_fetch(url, payload=None, method=GET, headers={},
          allow_truncated=False, follow_redirects=True,
          deadline=10.0, *args, **kwargs):
  return old_fetch(url, payload, method, headers, allow_truncated,
                   follow_redirects, deadline, *args, **kwargs)
urlfetch.fetch = new_fetch
+3

. GAE API.

# -*- coding: utf-8 -*-
from google.appengine.api import urlfetch

import settings


def fetch(*args, **kwargs):
    """
    Base fetch func with default deadline settings
    """
    fetch_kwargs = {
        'deadline': settings.URL_FETCH_DEADLINE
    }
    fetch_kwargs.update(kwargs)
    return urlfetch.fetch(
        *args, **fetch_kwargs
    )
+1

, :

from google.appengine.api import urlfetch
import urllib, urllib2


class MyClass():

    def __init__(self):
        urlfetch.set_default_fetch_deadline(10)

urllib2 CookieJar,

response = self.opener.open(self.url_login, data_encoded)

, 0.1

0

- ? :

Python 2.3 , . , -. . - httplib urllib2. , - , :

import socket
import urllib2

# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# this call to urllib2.urlopen now uses the default timeout
# we have set in the socket module
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)

, GAE , !

Edit:

urllib2 :

- , ( , - ). HTTP, HTTPS, FTP FTPS connections.connections.

-3
source

All Articles