def http_connection(url): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ parsed = urlparse(url) if parsed.scheme == 'http': conn = HTTPConnection(parsed.netloc) elif parsed.scheme == 'https': conn = HTTPSConnection(parsed.netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) return parsed, conn
def http_connection(url, proxy=None): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :param proxy: proxy to connect through, if any; None by default; str of the format 'http://127.0.0.1:8888' to set one :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ url = encode_utf8(url) parsed = urlparse(url) proxy_parsed = urlparse(proxy) if proxy else None if parsed.scheme == 'http': conn = HTTPConnection((proxy_parsed if proxy else parsed).netloc) elif parsed.scheme == 'https': conn = HTTPSConnection((proxy_parsed if proxy else parsed).netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) def putheader_wrapper(func): @wraps(func) def putheader_escaped(key, value): func(encode_utf8(key), encode_utf8(value)) return putheader_escaped conn.putheader = putheader_wrapper(conn.putheader) def request_wrapper(func): @wraps(func) def request_escaped(method, url, body=None, headers=None): url = encode_utf8(url) if body: body = encode_utf8(body) func(method, url, body=body, headers=headers or {}) return request_escaped conn.request = request_wrapper(conn.request) if proxy: conn._set_tunnel(parsed.hostname, parsed.port) return parsed, conn
def http_connection(url, proxy=None): """ Make an HTTPConnection or HTTPSConnection :param url: url to connect to :param proxy: proxy to connect through, if any; None by default; str of the format 'http://127.0.0.1:8888' to set one :returns: tuple of (parsed url, connection object) :raises ClientException: Unable to handle protocol scheme """ parsed = urlparse(url) proxy_parsed = urlparse(proxy) if proxy else None if parsed.scheme == 'http': conn = HTTPConnection((proxy_parsed if proxy else parsed).netloc) elif parsed.scheme == 'https': conn = HTTPSConnection((proxy_parsed if proxy else parsed).netloc) else: raise ClientException('Cannot handle protocol scheme %s for url %s' % (parsed.scheme, repr(url))) if proxy: conn._set_tunnel(parsed.hostname, parsed.port) return parsed, conn