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


Python http_headers.Headers类代码示例

本文整理汇总了Python中twisted.web.http_headers.Headers的典型用法代码示例。如果您正苦于以下问题:Python Headers类的具体用法?Python Headers怎么用?Python Headers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: encode_headers

def encode_headers(headers):
    twisted_headers = Headers()
    for k, v in headers.items():
        key = k.encode('ascii', 'ignore')
        val = v.encode('ascii', 'ignore')
        twisted_headers.addRawHeader(key, val)
    return twisted_headers
开发者ID:JvanDalfsen,项目名称:calvin-base,代码行数:7,代码来源:http_client.py

示例2: http_get

def http_get(reactor, url, accept=None):
    agent = client.Agent(reactor)
    headers = Headers()
    if accept is not None:
        headers.addRawHeader('Accept', str(accept))
    d = agent.request('GET', url, headers=headers)
    return _handle_agent_response(d)
开发者ID:nunb,项目名称:shinysdr,代码行数:7,代码来源:testutil.py

示例3: request

    def request(self, method, url, **kwargs):
        method = method.upper()

        # Join parameters provided in the URL
        # and the ones passed as argument.
        params = kwargs.get('params')
        if params:
            url = _combine_query_params(url, params)

        # Convert headers dictionary to
        # twisted raw headers format.
        headers = kwargs.get('headers')
        if headers:
            if isinstance(headers, dict):
                h = Headers({})
                for k, v in headers.iteritems():
                    if isinstance(v, str):
                        h.addRawHeader(k, v)
                    else:
                        h.setRawHeaders(k, v)

                headers = h
        else:
            headers = Headers({})

        # Here we choose a right producer
        # based on the parameters passed in.
        bodyProducer = None
        data = kwargs.get('data')
        files = kwargs.get('files')
        if files:
            # If the files keyword is present we will issue a
            # multipart/form-data request as it suits better for cases
            # with files and/or large objects.
            files = list(_convert_files(files))
            boundary = uuid.uuid4()
            headers.setRawHeaders(
                'content-type', [
                    'multipart/form-data; boundary=%s' % (boundary,)])
            if data:
                data = _convert_params(data)
            else:
                data = []

            bodyProducer = multipart.MultiPartProducer(
                data + files, boundary=boundary)
        elif data:
            # Otherwise stick to x-www-form-urlencoded format
            # as it's generally faster for smaller requests.
            if isinstance(data, (dict, list, tuple)):
                headers.setRawHeaders(
                    'content-type', ['application/x-www-form-urlencoded'])
                data = urlencode(data, doseq=True)
            bodyProducer = IBodyProducer(data)

        d = self._agent.request(
            method, url, headers=headers,
            bodyProducer=bodyProducer)

        return d
开发者ID:itsjustpiper,项目名称:treq,代码行数:60,代码来源:client.py

示例4: test_initializer

 def test_initializer(self):
     """
     The header values passed to L{Headers.__init__} can be retrieved via
     L{Headers.getRawHeaders}.
     """
     h = Headers({b"Foo": [b"bar"]})
     self.assertEqual(h.getRawHeaders(b"foo"), [b"bar"])
开发者ID:pelluch,项目名称:VTK,代码行数:7,代码来源:test_http_headers.py

示例5: headers

    def headers(self, rawHeaders):
        # type: (RawHeaders) -> IMutableHTTPHeaders
        headers = Headers()
        for rawName, rawValue in rawHeaders:
            headers.addRawHeader(rawName, rawValue)

        return HTTPHeadersWrappingHeaders(headers=headers)
开发者ID:notoriousno,项目名称:klein,代码行数:7,代码来源:test_headers_compat.py

示例6: request

    def request(self, method, uri, headers=None, bodyProducer=None):
        parsedURI = client._parse(uri)
        
        host_addr = address.IPv4Address('TCP', parsedURI.host, parsedURI.port)
        
        
        # ripped from _AgentBase._requestWithEndpoint
        if headers is None:
            headers = Headers()
        if not headers.hasHeader('host'):
            headers = headers.copy()
            headers.addRawHeader(
                'host', self._computeHostValue(parsedURI.scheme, parsedURI.host,
                                               parsedURI.port))
        request = client.Request(method, parsedURI.path, headers, bodyProducer,
                                 persistent=False)

        c = ClientProtocol(request)
        
        # ouch
        self.root.putChild('', self.root)
        
        server = Site(self.root).buildProtocol(self.addr)
        loopbackAsync(server, c, host_addr, self.addr)
        return c.response.addBoth(self._done, c)
开发者ID:tomprince,项目名称:agentforhire,代码行数:25,代码来源:badhack.py

示例7: url_upload_data_async

def url_upload_data_async(url, files={}, data={}, progressCallback=None, doneCallback=None):

    def produce_finished(data):
        print 'produce finished',data
    def produce_error(error):
        print 'produce error', error

    producerDeferred = Deferred()
    producerDeferred.addCallback(produce_finished)
    producerDeferred.addErrback(produce_error)

    def receive_finished(data):
        print 'recieve finished', data
        doneCallback(data)

    def receive_error(error):
        Logger.error('Problem uploading file')
        print 'recieve error', error

    receiverDeferred = Deferred()
    receiverDeferred.addCallback(receive_finished)
    receiverDeferred.addErrback(receive_error)

    producer = MultiPartProducer(files, data, progressCallback, producerDeferred)
    receiver = StringReceiver(receiverDeferred)

    agent = Agent(reactor)
    headers = Headers({'User-Agent': ['Twisted Web Client Example']})
    headers.addRawHeader("Content-Type", "multipart/form-data; boundary=%s" % producer.boundary)

    requestDeffered = agent.request('POST', url, headers, producer)
    requestDeffered.addCallback(lambda response: response.deliverBody(receiver))
开发者ID:insiderr,项目名称:insiderr-app,代码行数:32,代码来源:http_utils.py

示例8: _make_request

    def _make_request(self):
        metrics = []
        for x in xrange(HTTP_BATCH):
            if not self._mq:
                break
            metrics.append(self._mq.popleft())
        if not metrics:
            return defer.succeed(None)

        serialized_metrics = json.dumps({"metrics": metrics})
        body_writer = StringProducer(serialized_metrics)

        headers = Headers({
            'User-Agent': ['Zenoss Metric Publisher: %s' % self._agent_suffix],
            'Content-Type': ['application/json']})

        if self._needsAuth and not self._authenticated:
            log.info("Adding auth for metric http post %s", self._url)
            headers.addRawHeader('Authorization',
                                 basic_auth_string_content(self._username, self._password))

        d = self._agent.request(
            'POST', self._url, headers,
            body_writer)

        d.addCallbacks(self._metrics_published, errback=self._publish_failed,
        callbackArgs = [len(metrics), len(self._mq)], errbackArgs = [metrics])
        d.addCallbacks(self._response_finished, errback=self._publish_failed,
                       errbackArgs = [metrics])

        return d
开发者ID:bbc,项目名称:zenoss-prodbin,代码行数:31,代码来源:publisher.py

示例9: test_initializer

 def test_initializer(self):
     """
     The header values passed to L{Headers.__init__} can be retrieved via
     L{Headers.getRawHeaders}.
     """
     h = Headers({'Foo': ['bar']})
     self.assertEqual(h.getRawHeaders('foo'), ['bar'])
开发者ID:AndyPanda95,项目名称:python-for-android,代码行数:7,代码来源:test_http_headers.py

示例10: post

def post(url, data=None, on_created=None, on_error=None):
    errback = on_error or make_errback(frames_back=2)
    try:
        def handle_response(response):
            if response.code == 200:
                callback = partial(
                    _handle_post,
                    on_created=on_created,
                    on_error=errback)
                response.deliverBody(JsonReceiver.create(callback, errback))
            else:
                errback('returned %s' % response.code)

        agent = Agent(reactor)
        headers = Headers(get_auth_headers())
        headers.addRawHeader('Content-Type', 'application/json')
        if not data:
            data = {'dummy': 1}
        d = agent.request(
            'POST',
            add_params(url, rid=uuid4().hex),
            headers=headers,
            bodyProducer=JsonProducer(data) if data else None)
        d.addCallbacks(handle_response, errback)
    except Exception as ex:
        errback('error %s' % ex)
开发者ID:insiderr,项目名称:insiderr-app,代码行数:26,代码来源:post.py

示例11: obtain_auth_token

 def obtain_auth_token(self):
     """Return a valid Microsoft Cognitive Services authentication
     token, obtained with the current subscription key if necessary.
     """
     start_time = time.time()
     if self.auth_token is None or self.token_expiry < start_time:
         headers = Headers()
         headers.addRawHeader('Ocp-Apim-Subscription-Key',
                              self.subscription_key)
         headers.addRawHeader('Content-Length', '0')
         response = yield self.agent.request(
             'POST', AUTH_URL, headers=headers)
         if response.code != 200:
             data = yield readBody(response)
             self.log.error(
                 'Could not authenticate to Microsoft Cognitive '
                 'Services: {data}', data=data)
             raise UserVisibleError(
                 'Could not authenticate to Microsoft Cognitive '
                 'Services. Try again later.')
         # Coerce the access token to a byte string to avoid problems
         # inside Twisted's header handling code down the line.
         self.auth_token = (
             (yield readBody(response)).strip().decode('ascii'))
         self.token_expiry = start_time + AUTH_TOKEN_TTL
     returnValue(self.auth_token)
开发者ID:kxz,项目名称:omnipresence,代码行数:26,代码来源:__init__.py

示例12: request

 def request(self, method, uri, headers=None, data=None):
     if data is not None:
         if headers is None:
             headers = Headers()
         headers.removeHeader('Content-Type')
         headers.addRawHeader('Content-Type', 'application/json; charset=utf-8')
     return BasicAgent.request(self, method, uri, headers, data)
开发者ID:pantheon-systems,项目名称:tx_clients,代码行数:7,代码来源:http.py

示例13: request

    def request(self, url, method='GET', data=None):
        d = defer.Deferred()
        _headers = {}
        headers = None

        if method == 'GET':
            data = None
        elif method == 'POST':
            _headers['Content-Type'] = 'application/x-www-form-urlencoded'
        else:
            d.errback(Exception('Not Support Method:%s.' % method))
            return

        if _headers:
            headers = Headers()
            for k, v in _headers.iteritems():
                headers.setRawHeaders(urllib.quote(k), [urllib.quote(v)])

        if data:
            data = StrBodyProducer(data)
        #print 'Req url:', url, ' Head:', headers
        self.agent.request(
                method,
                url,
                headers,
                data).addCallback(self.received, d).addErrback(
                    self.errback, d)

        return d
开发者ID:anson-tang,项目名称:3dkserver,代码行数:29,代码来源:http.py

示例14: endpoint

def endpoint(endpoint, username='about', apikey=None, data=None):
    global url, user_agent
    def encode_body(data):
        if (data):
            if (isinstance(data, six.string_types)):
                data = json_loads(data)
            return ('POST', FileBodyProducer(six.StringIO(json_dumps(data))))
        else:
            return ('GET', None)
    address = make_endpoint_address(endpoint, username)
    headers = Headers({
        'Accept': ['application/json,*/*;q=0.8'],
        'Accept-Encoding': ['gzip,deflate,sdch'],
        'Connection': ['keep-alive'],
        'User-Agent': [user_agent],
        'Content-Type': ['application/json'],
    })
    if (apikey):
        headers.addRawHeader('Authorization', 'Basic ' + encode_apikey(str(apikey)))
    method, body = encode_body(data)
    agent = Agent(reactor)
    response = yield agent.request(method, address.encode('utf-8'), headers, body)
    if (response.code == 200):
        finished = Deferred()
        collector = WebBodyCollector(finished)
        response.deliverBody(collector)
        x = yield finished
        returnValue(json_loads(collector.body))
    else:
        raise RequestError(response.phrase)
开发者ID:TigerND,项目名称:gratipay-twisted,代码行数:30,代码来源:api.py

示例15: _doPush

	def _doPush( self, filename ):
		print 'beginning push of file:', filename
		producerDeferred = defer.Deferred()
		producerDeferred.addCallback( self._finished )
		producerDeferred.addErrback( self._error )
		
		multiPartProducer = MultiPartProducer(
			files={
				'metrics': filename,
			},
			data={
				'secret': agent_settings.PUSH_DETAILS['secret'],
			},
			callback=self._progress,
			deferred=producerDeferred
		)
	
		headers = Headers()
		headers.addRawHeader( 'Content-Type', 'multipart/form-data; boundary=%s' % multiPartProducer.boundary )
	
		agent = client.Agent( reactor )
		request = agent.request(
			'POST',
			agent_settings.PUSH_DETAILS['url'],
			headers,
			multiPartProducer
		)
		request.addCallback( self._responseReady, filename )
开发者ID:MagmaLabs,项目名称:cloud-agent,代码行数:28,代码来源:PushService.py


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