本文整理汇总了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
示例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)
示例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
示例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"])
示例5: headers
def headers(self, rawHeaders):
# type: (RawHeaders) -> IMutableHTTPHeaders
headers = Headers()
for rawName, rawValue in rawHeaders:
headers.addRawHeader(rawName, rawValue)
return HTTPHeadersWrappingHeaders(headers=headers)
示例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)
示例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))
示例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
示例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'])
示例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)
示例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)
示例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)
示例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
示例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)
示例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 )