当前位置: 首页>>代码示例>>Python>>正文


Python Request.add_header方法代码示例

本文整理汇总了Python中urllib.request.Request.add_header方法的典型用法代码示例。如果您正苦于以下问题:Python Request.add_header方法的具体用法?Python Request.add_header怎么用?Python Request.add_header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在urllib.request.Request的用法示例。


在下文中一共展示了Request.add_header方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: imgUrlToFile

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def imgUrlToFile(url, referer, filename):
    f = open(filename,'wb') # create file
    request = Request(url)
    request.add_header('Referer', referer) # set referer #헤더를 추가해서 다음에서 접속하는 효과를
    response = urlopen(request) # connet
    f.write(response.read()) # save buffer to file
    f.close()
开发者ID:subper,项目名称:pythonteamproject10,代码行数:9,代码来源:Crawl.py

示例2: shorten

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def shorten(url):
    """
    与えられたURLをgoo.glを使って短縮します

    settings.GOOGLE_URL_SHORTENER_API_KEYが設定されているときはそれを使って短縮します。
    詳細は以下を参照してください
    https://developers.google.com/url-shortener/v1/getting_started#auth
    """
    api_key = getattr(settings, 'GOOGLE_URL_SHORTENER_API_KEY', None)
    try:
        api_url = API_URL
        if api_key:
            api_url = '{}?key={}'.format(api_url, api_key)
        data = json.dumps({'longUrl': url})
        data = data.encode('utf-8')
        request = Request(api_url, data)
        request.add_header('Content-Type', 'application/json')
        r = urlopen(request)
        json_string = r.read().decode("utf-8")
        return json.loads(json_string)['id']
    except Exception as e:
        # fail silently
        logger = logging.getLogger('kawaz.core.utils')
        logger.exception("Failed to shorten `{}`".format(url))
        return url
开发者ID:fordream,项目名称:Kawaz3rd,代码行数:27,代码来源:shortenurl.py

示例3: parase_fq_factor

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def parase_fq_factor(code):
    symbol = _code_to_symbol(code)
    try:
        request = Request(ct.HIST_FQ_FACTOR_URL%(symbol))
        request.add_header("User-Agent", ct.USER_AGENT)
        text = urlopen(request, timeout=20).read()
        text = text[1:len(text)-1]
        text = text.decode('utf-8') if ct.PY3 else text
        text = text.replace('{_', '{"')
        text = text.replace('total', '"total"')
        text = text.replace('data', '"data"')
        text = text.replace(':"', '":"')
        text = text.replace('",_', '","')
        text = text.replace('_', '-')
        text = json.loads(text)
        df = pd.DataFrame({'date':list(text['data'].keys()), 'fqprice':list(text['data'].values())})
        df['date'] = df['date'].map(_fun_except) # for null case
        if df['date'].dtypes == np.object:
            df['date'] = df['date'].astype(np.str)
        df = df.drop_duplicates('date')
        df = df.sort('date', ascending=False)
        df = df.set_index("date")
        df['fqprice'] = df['fqprice'].astype(float)
        return df
    except Exception as e:
        print(e)
开发者ID:hezhenke,项目名称:AshareBackTest,代码行数:28,代码来源:fq.py

示例4: cypher

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def cypher(query, **args):

    data = {
        "query": query,
        "params": args
    }

    data = json.dumps(data)

    req = Request(
        url="http://localhost:7474/db/data/cypher",
        data=data)

    req.add_header('Accept', 'application/json')
    req.add_header('Content-Type', 'application/json')

    try:
        resp = urlopen(req)
    except HTTPError as err:
        if err.code == 400:
            err = json.loads(err.read())
            return print_error('', query, err)
        else:
            print(err)

        return
    else:
        resp = json.loads(resp.read())
        columns = resp['columns']
        rows = resp['data']
        print_table(columns, rows)
开发者ID:jRiest,项目名称:sublime-cypher,代码行数:33,代码来源:Cypher.py

示例5: get_http_data

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def get_http_data(url, method="GET", header="", data=""):
    """ Get the page to parse it for streams """
    request = Request(url)
    request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')

    if len(header) > 0:
        request.add_header('Content-Type', header)
    if len(data) > 0:
        request.add_data(data)
    try:
        response = urlopen(request)
    except HTTPError as e:
        log.error("Something wrong with that url")
        log.error("Error code: %s" % e.code)
        sys.exit(5)
    except URLError as e:
        log.error("Something wrong with that url")
        log.error("Error code: %s" % e.reason)
        sys.exit(5)
    except ValueError as e:
        log.error("Try adding http:// before the url")
        sys.exit(5)
    if sys.version_info > (3, 0):
        data = response.read().decode('utf-8')
    else:
        try:
            data = response.read()
        except socket.error as e:
            log.error("Lost the connection to the server")
            sys.exit(5)
    response.close()
    return data
开发者ID:akeks,项目名称:svtplay-dl,代码行数:34,代码来源:svtplay_dl.py

示例6: make_request

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
 def make_request(self, url, method, params):
     """Makes an HTTP request and returns the response."""
     request = Request(url, params)
     for key, val in items(self.http_headers):
         request.add_header(key, val)
     response = urlopen(request)  # user catches errors.
     return response.read()
开发者ID:Birdbird,项目名称:celery,代码行数:9,代码来源:http.py

示例7: data

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
    def data(self):
        if not hasattr(self, '_data'):
            request = URLRequest(self.url)

            # Look in the cache for etag / last modified headers to use
            # TODO: "expires" header could be supported
            if self.env and self.env.cache:
                headers = self.env.cache.get(
                    ('url', 'headers', self.url))
                if headers:
                    etag, lmod = headers
                    if etag: request.add_header('If-None-Match', etag)
                    if lmod: request.add_header('If-Modified-Since', lmod)

            # Make a request
            try:
                response = urlopen(request)
            except HTTPError as e:
                if e.code != 304:
                    raise
                    # Use the cached version of the url
                self._data = self.env.cache.get(('url', 'contents', self.url))
            else:
                with contextlib.closing(response):
                    self._data = response.read()

                # Cache the info from this request
                if self.env and self.env.cache:
                    self.env.cache.set(
                        ('url', 'headers', self.url),
                        (response.headers.getheader("ETag"),
                         response.headers.getheader("Last-Modified")))
                    self.env.cache.set(('url', 'contents', self.url), self._data)
        return self._data
开发者ID:thebbgroup,项目名称:webassets,代码行数:36,代码来源:merge.py

示例8: authenticate

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
    def authenticate(self, request):
        adhocracy_base_url = settings.PC_SERVICES["references"]["adhocracy_api_base_url"]
        user_path = request.META.get("HTTP_X_USER_PATH")
        user_token = request.META.get("HTTP_X_USER_TOKEN")
        user_url = urljoin(adhocracy_base_url, user_path)

        if user_path is None and user_token is None:
            return None
        elif user_path is None or user_token is None:
            raise exceptions.AuthenticationFailed("No `X-User-Path` and `X-User-Token` header provided.")

        request = Request("%s/principals/groups/gods" % adhocracy_base_url)
        request.add_header("X-User-Path", user_path)
        request.add_header("X-User-Token", user_token)

        response = urlopen(request)

        if response.status == 200:
            content_type, params = parse_header(response.getheader("content-type"))
            encoding = params["charset"].lower()
            if content_type != "application/json":
                exceptions.AuthenticationFailed("Adhocracy authentification failed due wrong response.")
            resource_as_string = response.read().decode(encoding)
            gods_group_resource = json.loads(resource_as_string)
            gods = gods_group_resource["data"]["adhocracy_core.sheets.principal.IGroup"]["users"]

            if user_url in gods:
                is_god = True
            else:
                is_god = False

            return AdhocracyUser(user_path, is_god), None
        else:
            raise exceptions.AuthenticationFailed("Adhocracy authentification failed due invalid credentials.")
开发者ID:almey,项目名称:policycompass-services,代码行数:36,代码来源:auth.py

示例9: _CallHttp

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
    def _CallHttp(self, method, handler, data=''):
        method = method.upper()
        request_uri = self._BuildUri(handler)
        req = Request(request_uri, method=method)
        if isinstance(data, collections.Mapping):
            req.add_header('content-type', 'application/json')
            data = json.dumps(data, ensure_ascii=False)
        data = data.encode('utf-8')
        hmac = self._HmacForRequest(method,
                                    urlparse(request_uri).path,
                                    data)
        req.add_header(HMAC_HEADER, hmac)
        req.data = data
        try:
            resp = urlopen(req)
        except HTTPError as err:
            readData = err.read().decode('utf-8')
            print('[C++YouCompleteMe] Error from ycmd server: {}'.format(
                json.loads(readData).get('message', '')))
            return ''

        readData = resp.read()
        # self._ValidateResponseObject(
            # readData, resp.getheader(HMAC_HEADER).encode('utf-8'))

        return readData.decode('utf-8')
开发者ID:imaveek,项目名称:CppYCM,代码行数:28,代码来源:ycmd_handler.py

示例10: run

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
    def run(self):
        result = None
        try:
            # make api request
            in_data = open(self.png_file, 'rb').read()
            raw_key = ('api:' + self.api_key).encode('ascii')
            enc_key = standard_b64encode(raw_key).decode('ascii')
            request = Request(TINYPNG_URL.encode('utf-8'), in_data)
            request.add_header('Authorization', 'Basic %s' % enc_key)
            request.add_header('Content-Type', 'application/octet-stream')

            result = urlopen(request)

            # download shrinked file
            body = result.read().decode('utf8')
            print body
            self.response = json.loads(body)
            out_data = urlopen(self.response['output']['url']).read()
            if len(out_data) > 0:
                f = open(self.png_file, 'wb')
                f.write(out_data)

        except HTTPError as e:
            self.error = 'HTTP error %s contacting TinyPNG API' % (str(e.code))
            try:
                body = json.loads(e.read().decode('utf8'))
                self.error += ' (%s)' % body['message']
            except ValueError as e:
                pass
开发者ID:98ki,项目名称:sublime-tinypng,代码行数:31,代码来源:TinyPNG.py

示例11: open_with_auth2

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def open_with_auth2(url):
    """
    Open a urllib2 request, handling HTTP authentication
    In this version, user-agent is ignored
    """

    scheme, netloc, path, params, query, frag = urlparse(url)

    if scheme in ('http', 'https'):
        auth, host = splituser(netloc)
    else:
        auth = None

    if auth:
        auth = "Basic " + unquote(auth).encode('base64').strip()
        new_url = urlunparse((scheme, host, path, params, query, frag))
        request = Request(new_url)
        request.add_header("Authorization", auth)
    else:
        request = Request(url)

    # request.add_header('User-Agent', user_agent)
    fp = urlopen(request)

    if auth:
        # Put authentication info back into request URL if same host,
        # so that links found on the page will work
        s2, h2, path2, param2, query2, frag2 = urlparse(fp.url)
        if s2 == scheme and h2 == host:
            fp.url = urlunparse((s2, netloc, path2, param2, query2, frag2))

    return fp
开发者ID:openalea,项目名称:deploy,代码行数:34,代码来源:gforge_util.py

示例12: get_page

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
 def get_page(self,url):
     request = Request(url)
     request.add_header(GoogleSearch._user_agent[0],GoogleSearch._user_agent[1])
     response = urlopen(request)
     html = response.read()
     response.close()
     return html
开发者ID:seregat,项目名称:Vsearch_engine,代码行数:9,代码来源:GoogleSearchService.py

示例13: get_page

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def get_page(url,ip,conn_type):
    """
    Request the given URL and return the response page, using the cookie jar.

    @type  url: str
    @param url: URL to retrieve.

    @rtype:  str
    @return: Web page retrieved for the given URL.

    @raise IOError: An exception is raised on error.
    @raise urllib2.URLError: An exception is raised on error.
    @raise urllib2.HTTPError: An exception is raised on error.
    """
    request = Request(url)
    request.add_header('User-Agent',
                       'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)')
    cookie_jar.add_cookie_header(request)

    if ip != None and conn_type != None:
        proxy = urllib2.ProxyHandler({conn_type:ip})
        opener = urllib2.build_opener(proxy)
        urllib2.install_opener(opener)

    response = urlopen(request)
    cookie_jar.extract_cookies(response, request)
    html = response.read()
    response.close()
    cookie_jar.save()
    return html
开发者ID:mthbernardes,项目名称:google,代码行数:32,代码来源:__init__.py

示例14: nearest

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def nearest(coord, number=1, url_config=RequestConfig):
    """
    Useless function wrapping OSRM 'nearest' function,
    returning the reponse in JSON

    Parameters
    ----------
    coord : list/tuple of two floats
        (x ,y) where x is longitude and y is latitude
    number : int, optional
    url_config : osrm.RequestConfig, optional
        Parameters regarding the host, version and profile to use

    Returns
    -------
    result : dict
        The response from the osrm instance, parsed as a dict
    """
    host = check_host(url_config.host)
    url = ''.join([
        host, '/nearest/', url_config.version, '/', url_config.profile, '/',
         ','.join(map(str, coord)), '?number={}'.format(number)
    ])

    req = Request(url)
    if url_config.auth:
        req.add_header("Authorization", url_config.auth)
    rep = urlopen(req)
    parsed_json = json.loads(rep.read().decode('utf-8'))
    return parsed_json
开发者ID:ustroetz,项目名称:python-osrm,代码行数:32,代码来源:core.py

示例15: raw

# 需要导入模块: from urllib.request import Request [as 别名]
# 或者: from urllib.request.Request import add_header [as 别名]
def raw(ctx, method, url, datafp=None):
    """Do the raw http method call on url."""
        
    if method not in ("GET", "POST", "PUT", "DELETE"):
        raise ValueError("HTTP method '{}' is not known".format(method))
    
    if method in ("PUT", "DELETE"):
        raise NotImplementedError("HTTP method '{}' is not yet implemented".format(method))
    
    #TODO: we need a real debugging
    print("DEBUG: {} {}".format(method, url))

    data = None
    
    request = Request(url)
    if hasattr(request, method):
        request.method = method
    else:
        request.get_method = lambda: method

    if method == "POST":
        data = datafp.read() if datafp is not None else b""
        request.add_header("Content-Type", "application/octet-stream")
        
    response = ctx.opener.open(request, data)

    if response.getcode() != 200:
        raise NotImplementedError("non 200 responses are not yet implemented")
    
    return response
开发者ID:mvyskocil,项目名称:bslib,代码行数:32,代码来源:raw.py


注:本文中的urllib.request.Request.add_header方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。