Did I find the right examples for you? yes no

All Samples(1)  |  Call(0)  |  Derive(0)  |  Import(1)
Utility function that compresses the request and add the user agent header
if supplied.

:param str url: url to request
:param str data: data to add to the request
:param bool compress: if the response should be compressed
:param str agent: if supplied, the 'User-Agent' header we'll be added to
    the request
:return: the response handle

        def url_open(url, data=None, compress=True, agent=None):
    """
    Utility function that compresses the request and add the user agent header
    if supplied.

    :param str url: url to request
    :param str data: data to add to the request
    :param bool compress: if the response should be compressed
    :param str agent: if supplied, the 'User-Agent' header we'll be added to
        the request
    :return: the response handle
    """
    if data is None:
        request = urllib2.Request(url)
    else:
        request = urllib2.Request(url, data)

    if agent is not None:
        request.add_header('User-Agent', "Python {0}".format(agent))

    if compress:
        request.add_header('Accept-encoding', 'gzip')

    response = urllib2.urlopen(request)

    if response.info().get('Content-Encoding') == 'gzip':
        data_handle = cStringIO.StringIO(response.read())
        data_handle = gzip.GzipFile(fileobj=data_handle, mode='rb')
    else:
        data_handle = response

    return data_handle
        


src/m/g/mgkit-0.1.11/mgkit/net/__init__.py   mgkit(Download)
"""
Package with functions/classes used in accessing network resources
"""
from .utils import url_read, url_open