def urlopen(url, timeout=<NUM_LIT:20>, redirects=None):
scheme, host, path, params, query, frag = urlparse(url)<EOL>if not scheme in ('<STR_LIT:http>', '<STR_LIT>'):<EOL><INDENT>return urllib.request.urlopen(url)<EOL><DEDENT>if params: path = '<STR_LIT>' % (path, params)<EOL>if query:  path = '<STR_LIT>' % (path, query)<EOL>if frag:   path = '<STR_LIT>' % (path, frag)<EOL>if scheme == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>import M2Crypto<EOL><DEDENT>except ImportError:<EOL><INDENT>if not hasattr(socket, '<STR_LIT>'):<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>conn = TimeoutHTTPS(host, None, timeout)<EOL><DEDENT>else:<EOL><INDENT>ctx = M2Crypto.SSL.Context()<EOL>ctx.set_session_timeout(timeout)<EOL>conn = M2Crypto.httpslib.HTTPSConnection(host, ssl_context=ctx)<EOL>conn.set_debuglevel(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>conn = TimeoutHTTP(host, None, timeout)<EOL><DEDENT>conn.putrequest('<STR_LIT:GET>', path)<EOL>conn.putheader('<STR_LIT>', '<STR_LIT>')<EOL>conn.endheaders()<EOL>response = None<EOL>while <NUM_LIT:1>:<EOL><INDENT>response = conn.getresponse()<EOL>if response.status != <NUM_LIT:100>:<EOL><INDENT>break<EOL><DEDENT>conn._HTTPConnection__state = http.client._CS_REQ_SENT<EOL>conn._HTTPConnection__response = None<EOL><DEDENT>status = response.status<EOL>if status >= <NUM_LIT> and status < <NUM_LIT>:<EOL><INDENT>location = response.msg.getheader('<STR_LIT:location>')<EOL>if location is not None:<EOL><INDENT>response.close()<EOL>if redirects is not None and location in redirects:<EOL><INDENT>raise RecursionError(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT>if redirects is None:<EOL><INDENT>redirects = {}<EOL><DEDENT>redirects[location] = <NUM_LIT:1><EOL>return urlopen(location, timeout, redirects)<EOL><DEDENT>raise HTTPResponse(response)<EOL><DEDENT>if not (status >= <NUM_LIT:200> and status < <NUM_LIT>):<EOL><INDENT>raise HTTPResponse(response)<EOL><DEDENT>body = StringIO(response.read())<EOL>response.close()<EOL>return body<EOL>
A minimal urlopen replacement hack that supports timeouts for http.
       Note that this supports GET only.