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


Python Request.add_header方法代码示例

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


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

示例1: _http

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
 def _http(self, _method, _path, **kw):
     data = None
     params = None
     if _method == "GET" and kw:
         _path = "%s?%s" % (_path, _encode_params(kw))
     if _method in ["POST", "PATCH", "PUT"]:
         data = bytes(_encode_json(kw), "utf-8")
     url = "%s%s" % (_URL, _path)
     opener = build_opener(HTTPSHandler)
     request = Request(url, data=data)
     request.get_method = _METHOD_MAP[_method]
     if self._authorization:
         request.add_header("Authorization", self._authorization)
     if _method in ["POST", "PATCH", "PUT"]:
         request.add_header("Content-Type", "application/x-www-form-urlencoded")
     try:
         response = opener.open(request, timeout=TIMEOUT)
         is_json = self._process_resp(response.headers)
         if is_json:
             return _parse_json(response.read().decode("utf-8"))
     except HTTPError as e:
         is_json = self._process_resp(e.headers)
         if is_json:
             json = _parse_json(e.read().decode("utf-8"))
         else:
             json = e.read().decode("utf-8")
         req = JsonObject(method=_method, url=url)
         resp = JsonObject(code=e.code, json=json)
         if resp.code == 404:
             raise ApiNotFoundError(url, req, resp)
         raise ApiError(url, req, resp)
开发者ID:ColinDuquesnoy,项目名称:QCrash,代码行数:33,代码来源:github.py

示例2: request

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def request(url, args=None, byte_range=None, retries=HTTP_RETRY_COUNT, delay=HTTP_FETCH_DELAY):
    """Performs web request to url with optional retries, delay, and byte range.
    """
    _retry = False
    time.sleep(delay)

    try:
        if args is not None:
            enc_args = urlencode(args)
            enc_args = enc_args.encode('ascii') # needed for Python 3
        else:
            enc_args = None
        req = Request(url, data=enc_args)
        if byte_range is not None:
            req.add_header('Range', 'bytes=%d-%d' % byte_range)
        page = opener.open(req)
    except (HTTPError, URLError, socket.error, BadStatusLine) as e:
        if isinstance(e, HTTPError):
            if e.code in HTTP_PERM_ERRORCODES:  # do not retry these HTTP codes
                warn('request failed: %s.  will not retry.', e)
                raise
        if retries > 0:
            _retry = True
        else:
            raise

        if _retry:
            warn('request failed: %s (%d retries left) -- will retry in %ds...' % (e, retries, HTTP_RETRY_DELAY))
            return request(url=url, args=args, byte_range=byte_range, retries=retries-1, delay=HTTP_RETRY_DELAY)

    return contextlib.closing(page)
开发者ID:Trilarion,项目名称:gogrepo,代码行数:33,代码来源:gogrepo.py

示例3: getcommits_from_project

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def getcommits_from_project(project):
    global access_token
    url1 = 'https://api.github.com/user'
    request1=Request(url1)
    request1.add_header('Authorization', 'token %s' % access_token)
    response1 = urlopen(request1)
    result1 = json.load(response1)
    person = result1['login']
    repo_info=['Fasta','js2839']
    owner= repo_info[1]
    repo = repo_info[0]
    url = 'https://api.github.com/repos/'+owner+'/'+repo+'/commits'
    data=[]
    request = Request(url)
    request.add_header('Authorization', 'token %s' % access_token)
    response = urlopen(request)
    result = json.load(response)
    for i in range(len(result)):
        print 'result0'
        data.append([result[i]['commit']['message'], result[i]['commit']['author']['name'], result[i]['commit']['author']['date']])
        print data[i]
    for com in data:
        (per,sub_name)=getPercentage(com[0])
        err = save_to_db( per, sub_name, com[1], project, com[2])
    return 
开发者ID:ychsieh,项目名称:Actually,代码行数:27,代码来源:views.py

示例4: put_photo

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
    def put_photo(self, image, message=None, album_id=None, **kwargs):
        """Uploads an image using multipart/form-data
        image=File like object for the image
        message=Caption for your image
        album_id=None posts to /me/photos which uses or creates and uses
        an album for your application.
        """
        object_id = album_id or "me"
        #it would have been nice to reuse self.request; but multipart is messy in urllib
        post_args = {
				  'access_token': self.access_token,
				  'source': image,
				  'message': message
        }
        post_args.update(kwargs)
        content_type, body = self._encode_multipart_form(post_args)
        req = Request("https://graph.facebook.com/%s/photos" % object_id, data=body)
        req.add_header('Content-Type', content_type)
        try:
            data = urlopen(req).read()
        except HTTPError:
            e = sys.exc_info()[1] # for compatibility with python 2.X and 3.X
            data = e.read() # Facebook sends OAuth errors as 400, and urllib2 throws an exception, we want a GraphAPIError
        try:
            response = _parse_json(data)
            # Raise an error if we got one, but don't freak out if Facebook just gave us a Bool value
            if response and isinstance(response, dict) and response.get("error"):
                raise GraphAPIError(response["error"].get("code", 1),
                                    response["error"]["message"])
        except ValueError:
            response = data

        return response
开发者ID:ogier,项目名称:facebook-sdk,代码行数:35,代码来源:facebook.py

示例5: run_query

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def run_query(query_str):
	req = Request('{0}?{1}'.format(QUERY_BASE, urlencode({'q':query_str.replace(' ', '+')})))
	req.add_header('User-Agent', USER_AGENT)
	try:
		resp = urlopen(req)
	except URLError, e:
		return 0, None
开发者ID:ehudt,项目名称:mine-suggestions,代码行数:9,代码来源:sug_mining.py

示例6: attachment

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
	def attachment(self, attachid):
		"""Get an attachment by attachment_id

		@param attachid: attachment id
		@type  attachid: int

		@return: dict with three keys, 'filename', 'size', 'fd'
		@rtype: dict
		"""
		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['attach'].copy()
		qparams['id'] = attachid

		req_params = urlencode(qparams, True)
		req_url = urljoin(self.base, config.urls['attach'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.httpuser and self.httppassword:
			base64string = base64.encodestring('%s:%s' % (self.httpuser, self.httppassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		try:
			content_type = resp.info()['Content-type']
			namefield = content_type.split(';')[1]
			filename = re.search(r'name=\"(.*)\"', namefield).group(1)
			content_length = int(resp.info()['Content-length'], 0)
			return {'filename': filename, 'size': content_length, 'fd': resp}
		except:
			return {}
开发者ID:PabloCastellano,项目名称:pybugz,代码行数:34,代码来源:bugzilla.py

示例7: namedcmd

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
	def namedcmd(self, cmd):
		"""Run command stored in Bugzilla by name.

		@return: Result from the stored command.
		@rtype: list of dicts
		"""

		if not self.authenticated and not self.skip_auth:
			self.auth()

		qparams = config.params['namedcmd'].copy()
		# Is there a better way of getting a command with a space in its name
		# to be encoded as foo%20bar instead of foo+bar or foo%2520bar?
		qparams['namedcmd'] = quote(cmd)
		req_params = urlencode(qparams, True)
		req_params = req_params.replace('%25','%')

		req_url = urljoin(self.base, config.urls['list'])
		req_url += '?' + req_params
		req = Request(req_url, None, config.headers)
		if self.user and self.hpassword:
			base64string = base64.encodestring('%s:%s' % (self.user, self.hpassword))[:-1]
			req.add_header("Authorization", "Basic %s" % base64string)
		resp = self.opener.open(req)

		return self.extractResults(resp)
开发者ID:PabloCastellano,项目名称:pybugz,代码行数:28,代码来源:bugzilla.py

示例8: openURL

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def openURL(url_base, data, method='Get', cookies=None, username=None, password=None):
    ''' function to open urls - wrapper around urllib2.urlopen but with additional checks for OGC service exceptions and url formatting, also handles cookies and simple user password authentication'''
    url_base.strip() 
    lastchar = url_base[-1]
    if lastchar not in ['?', '&']:
        if url_base.find('?') == -1:
            url_base = url_base + '?'
        else:
            url_base = url_base + '&'
            
    if username and password:
        # Provide login information in order to use the WMS server
        # Create an OpenerDirector with support for Basic HTTP 
        # Authentication...
        passman = HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, url_base, username, password)
        auth_handler = HTTPBasicAuthHandler(passman)
        opener = urllib2.build_opener(auth_handler)
        openit = opener.open
    else:
        openit = urlopen
   
    try:
        if method == 'Post':
            req = Request(url_base, data)
        else:
            req=Request(url_base + data)
        if cookies is not None:
            req.add_header('Cookie', cookies)
        u = openit(req)
    except HTTPError, e: #Some servers may set the http header to 400 if returning an OGC service exception or 401 if unauthorised.
        if e.code in [400, 401]:
            raise ServiceException, e.read()
        else:
            raise e
开发者ID:sabman,项目名称:OWSLib,代码行数:37,代码来源:util.py

示例9: _http

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
 def _http(self, _method, _path, **kw):
     # import pdb;pdb.set_trace()
     data = None
     params = None
     if _method=='GET' and kw:
         _path = '%s?%s' % (_path, _encode_params(kw))
     if _method in ['POST', 'PATCH', 'PUT']:
         data = bytes(_encode_json(kw), 'utf-8')
     url = '%s%s' % (_URL, _path)
     opener = build_opener(HTTPSHandler)
     request = Request(url, data=data)
     request.get_method = _METHOD_MAP[_method]
     if self._authorization:
         request.add_header('Authorization', self._authorization)
     if _method in ['POST', 'PATCH', 'PUT']:
         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
     try:
         response = opener.open(request, timeout=TIMEOUT)
         is_json = self._process_resp(response.headers)
         if is_json:
             return _parse_json(response.read().decode('utf-8'))
     except HTTPError as e:
         is_json = self._process_resp(e.headers)
         if is_json:
             json = _parse_json(e.read().decode('utf-8'))
         else:
             json = e.read().decode('utf-8')
         req = JsonObject(method=_method, url=url)
         resp = JsonObject(code=e.code, json=json)
         if resp.code==404:
             raise ApiNotFoundError(url, req, resp)
         raise ApiError(url, req, resp)
开发者ID:litaotao,项目名称:git-statistic,代码行数:34,代码来源:github_api_v3.py

示例10: parseFeed

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
    def parseFeed(cls, url, lastModified=None, etag=None):
        '''
        Return tuple of feed object, last-modified, etag.
        '''
        req = Request(normalize_url(url))
        if lastModified:
            req.add_header('if-modified-since', lastModified)
        if etag:
            req.add_header('if-none-match', etag)
        resp = None
        try:
            resp = urlopen(req, None, 10)
        except HTTPError as error:
            # HTTP 304 not modifed raise an exception
            resp = error

        # url of local file returns empty code
        if resp.code and resp.code != 200:
            return None

        feedDoc = etree.parse(resp)
        feedType = None
        for ft in cls._feedTypes:
            if ft.accept(feedDoc):
                feedType = ft
                break
        if not feedType:
            raise ValueError('Cannot handle ' + feedDoc.getroot().tag)
        return (feedType(url, feedDoc),
                resp.headers.get('last-modified'),
                resp.headers.get('etag'))
开发者ID:goncha,项目名称:feed2mobi,代码行数:33,代码来源:feed.py

示例11: commitValues

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
 def commitValues(self, *args, **kwargs):
     """
 Set values given by kwargs
 set a variable by giving its name and its value as a parameter to this function 
 (name is given by its GET parameter in the balancer-manager)
 Example : set lf to 2 and ls to 10 :
   worker.commitValues(lf=2, ls=10)
 """
     srv = self.parentServer
     vh = self.parentVHost
     try:
         print ("[%s:%s - %s] Applying values %s" % (srv.ip, srv.port, vh.name, kwargs))
     except:
         pass
     if srv is None:
         return False
     url = self.actionURL
     for arg in iter(kwargs):
         val = kwargs[arg]
         if val is not None:
             url += "&%s=%s" % (arg, val)
     ## Caling url to set values given
     try:
         protocol = srv.secure and "https" or "http"
         req = Request("%s://%s:%s/%s" % (protocol, srv.ip, srv.port, url))
         if vh is not None and vh.name != "":
             req.add_header("Host", vh.name)
         urlopen(req)
     except:  ## Error
         return False
     return True
开发者ID:pchaussalet,项目名称:Apache-Cluster-Manager,代码行数:33,代码来源:core.py

示例12: send_cf_response

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def send_cf_response(event, context, response_status, reason=None,
                     response_data=None, physical_resource_id=None):
    """Responds to Cloudformation after a create/update/delete operation."""
    response_data = response_data or {}
    reason = reason or "See the details in CloudWatch Log Stream: " + \
        context.log_stream_name
    physical_resource_id = physical_resource_id or context.log_stream_name
    response_body = json.dumps(
        {
            'Status': response_status,
            'Reason': reason,
            'PhysicalResourceId': physical_resource_id,
            'StackId': event['StackId'],
            'RequestId': event['RequestId'],
            'LogicalResourceId': event['LogicalResourceId'],
            'Data': response_data
        }
    )

    opener = build_opener(HTTPHandler)
    request = Request(event["ResponseURL"], data=response_body)
    request.add_header("Content-Type", "")
    request.add_header("Content-Length", len(response_body))
    request.get_method = lambda: 'PUT'
    try:
        response = opener.open(request)
        print("Status code: {}".format(response.getcode()))
        print("Status message: {}".format(response.msg))
        return True
    except HTTPError as exc:
        print("Failed executing HTTP request: {}".format(exc.code))
        return False
开发者ID:humilis,项目名称:humilis-lambdautils,代码行数:34,代码来源:utils.py

示例13: login

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
    def login(self):
        # Get the hidden form fields
        r = self.opener.open(URL.login)
        if r.url.count("/loggetinn.aspx"):
            # We got redirected, and our session and stuff are still valid
            return
        html = r.read()
        # OK, we know these field data
        data = {}
        data["txtBrukernavn"] = USER
        data["txtPassord"] = PW
        data["chkLagre"] = "On"
        data["btnLoggInn"] = "Logg inn"

        data["__EVENTTARGET"] = ""
        data["__EVENTARGUMENT"] = ""
        # This crap we don't know what is, but it was a 
        # hidden field, so we could pass it on
        #viewstate = re.search(r'<input type="hidden" name="__VIEWSTATE" value="([^"]+)" />', html)
        # but it does work without        
        #data["__VIEWSTATE"] = viewstate.group(1)

        request = Request(URL.login, data=urlencode(data))
        request.add_header("Referer", URL.login)
        r = self.opener.open(request, data=urlencode(data))
        self.cj.save()
开发者ID:stain,项目名称:scrapbook,代码行数:28,代码来源:nrk.py

示例14: fetch_url

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
def fetch_url(url, referer=None, retries=1, dimension=False):
    """
    """
    cur_try = 0
    nothing = None if dimension else (None, None)
    url = clean_url(url)

    if not url.startswith(('http://', 'https://')):
        return nothing

    while True:
        try:
            req = Request(url)
            req.add_header('User-Agent', USERAGENT)
            if referer:
                req.add_header('Referer', referer)

            opener = build_opener()
            open_req = opener.open(req, timeout=5)

            # if we only need the dimension of the image, we may not
            # need to download the entire thing
            if dimension:
                content = open_req.read(chunk_size)
            else:
                content = open_req.read()

            content_type = open_req.headers.get('content-type')

            if not content_type:
                return nothing

            if 'image' in content_type:
                p = ImageFile.Parser()
                new_data = content
                while not p.image and new_data:
                    try:
                        p.feed(new_data)
                    except IOError, e:
                        # pil failed to install, jpeg codec broken
                        # **should work if you install via pillow
                        print ('***jpeg misconfiguration! check pillow or pil'
                                'installation this machine: %s' % str(e))
                        p = None
                        break
                    new_data = open_req.read(chunk_size)
                    content += new_data

                if p is None:
                    return nothing
                # return the size, or return the data
                if dimension and p.image:
                    return p.image.size
                elif dimension:
                    return nothing
            elif dimension:
                # expected an image, but didn't get one
                return nothing

            return content_type, content
开发者ID:michaelhood,项目名称:newspaper,代码行数:62,代码来源:images.py

示例15: describe

# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import add_header [as 别名]
    def describe(self, s_or_po, initBindings={}, initNs={}):
        """
        Executes a SPARQL describe of resource

        :param s_or_po:  is either

          * a subject ... should be a URIRef
          * a tuple of (predicate,object) ... pred should be inverse functional
          * a describe query string

        :param initBindings: A mapping from a Variable to an RDFLib term (used
            as initial bindings for SPARQL query)
        :param initNs: A mapping from a namespace prefix to a namespace
        """
        if isinstance(s_or_po, str):
            query = s_or_po
            if initNs:
                prefixes = ''.join(["prefix %s: <%s>\n" % (p, n)
                                    for p, n in initNs.items()])
                query = prefixes + query
        elif isinstance(s_or_po, URIRef) or isinstance(s_or_po, BNode):
            query = "describe %s" % (s_or_po.n3())
        else:
            p, o = s_or_po
            query = "describe ?s where {?s %s %s}" % (p.n3(), o.n3())
        query = dict(query=query)

        url = self.url + "?" + urlencode(query)
        req = Request(url)
        req.add_header('Accept', 'application/rdf+xml')
        log.debug("opening url: %s\n  with headers: %s" %
                  (req.get_full_url(), req.header_items()))
        subgraph = ConjunctiveGraph()
        subgraph.parse(urlopen(req))
        return subgraph
开发者ID:gjhiggins,项目名称:RDFAlchemy,代码行数:37,代码来源:__init__.py


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