why can you not use something along the following: (note timeoutsocket is part of python 2.4) robert class Connection(httplib.HTTPConnection): def __init__(self, host, port=None, strict=None, scheme="http", timeout=5): self.scheme = scheme if scheme == "http": self.default_port = httplib.HTTP_PORT elif scheme == "https": self.default_port = httplib.HTTPS_PORT else: raise ValueError, "Invalid scheme '%s'" % (scheme,) httplib.HTTPConnection.__init__(self, host, port, strict) self.timeout = timeout def connect(self): # We want to connect in normal blocking mode, else Python on Windows # will timeout even for 'connection refused' style errors (fixed in # Python 2.4 on Windows) if self.scheme == "http": httplib.HTTPConnection.connect(self) elif self.scheme == "https": # Clone of httplib.HTTPSConnection.connect sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.host, self.port)) key_file = cert_file = None ssl = socket.ssl(sock, key_file, cert_file) self.sock = httplib.FakeSocket(sock, ssl) else: raise ValueError, "Invalid scheme '%s'" % (self.scheme,) # Once we have connected, set the timeout. self.sock.settimeout(self.timeout) Michael Vartanyan wrote:
Hello All,
This is probably more a Python question but maybe you will have a quick solution for me - I guess the greatest *multi-threaded* Python application provides the greatest basis for this problem domain :-)
The situation: external method that is doing a http request using urllib2. I don't care about the response, and whether there was any response at all, I just need to send the request through - thus I want this request to time out very fast (let it be 2 seconds). I found no documented way to set the timeout for urllib2, after some googling I found an advice to manipulate the timeout on the lower-level socket module, something like this:
import socket import urllib2
def do_request(): timeout = 2 socket.setdefaulttimeout(timeout) req = urllib2.Request(url='http://my.site.com/do_something_quick') response = urllib2.urlopen(req)
The problem is this way this default timeout is set for _all_ new socket created by this Python process using the socket module. Even if I return the default to its previous state after the request it won't help me much - there are three more threads in my Zope that should be able to work with default timeout. So there are two possible solutions - to find a (preferably documented) way of accessing and parameterizing the socket object created by urllib2 to make a request or to find a way to isolate(??) global module settings between Zope threads.
Zope 2.8.3, Python 2.4.2, FreeBSD 4.10 if this is relevant.
Any hints/TFMs?
Many thanks Michael
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )