本文整理汇总了Python中twisted.web.http_headers.Headers.items方法的典型用法代码示例。如果您正苦于以下问题:Python Headers.items方法的具体用法?Python Headers.items怎么用?Python Headers.items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.http_headers.Headers
的用法示例。
在下文中一共展示了Headers.items方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import items [as 别名]
def call(self, url, method='GET', headers={}):
headers = Headers({
key: [value] for key, value in headers.items()
})
d = self.agent.request(
method, url, headers, None
)
def cbRequest(response):
finished = defer.Deferred()
response.deliverBody(
BodyReceiver(finished, response)
)
return finished
d.addCallback(cbRequest)
return d
示例2: _request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import items [as 别名]
def _request(self, endpoint, method, data=None):
body = None
params = {}
if method == "GET" and isinstance(data, dict):
endpoint = endpoint + '?' + urllib.urlencode(data)
else:
if isinstance(data, dict):
body = urllib.urlencode(data)
else:
body = data
request = oauth.Request.from_consumer_and_token(self.consumer,
http_method=method, http_url=endpoint, parameters=params)
request.sign_request(self.signature, self.consumer, None)
headers = request.to_header(self.realm)
headers['User-Agent'] = 'SimpleGeo Twisted Client v%s' % simplegeo.API_VERSION
headers = Headers(dict([(k, [v]) for k, v in headers.items()]))
agent = client.Agent(reactor)
response = yield agent.request(method, endpoint, headers, (body and StringProducer(body)))
body = yield receive_body(response)
if body: # Empty body is allowed.
try:
body = json.loads(body)
except ValueError:
raise DecodeError(resp, body)
if str(response.code)[0] != '2':
code = str(response.code)
message = body
if isinstance(body, dict):
if 'code' in body:
code = body['code']
if 'message' in body:
message = body['message']
raise simplegeo.APIError(code, message, response.headers)
returnValue(body)
示例3: make_request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import items [as 别名]
def make_request(method, url=None, headers=None, *args, **kwargs):
""" Makes a request """
headers = Headers(dict([(k, [v]) for k, v in headers.items()]))
formatter = None
if 'formatter' in kwargs:
formatter = kwargs.get('formatter')
del kwargs['formatter']
if not formatter:
def _nothing(result):
return result
formatter = _nothing
params = kwargs.get('params', None)
if params:
params = urllib.urlencode(params)
url = _full_url(url, params)
body = kwargs.get('data')
#print method, url, headers, body
contextFactory = WebClientContextFactory()
agent = Agent(reactor, contextFactory)
d = agent.request(
method,
url,
headers,
body)
load_body = True
if method.upper() in ['HEAD', 'DELETE']:
load_body = False
d.addCallback(complete_request, formatter, load_body=load_body)
d.addErrback(print_error)
return d
示例4: _upload_media
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import items [as 别名]
def _upload_media(self, uri, media, params):
boundary = 'txtwitter'
file_field = 'media'
body = ''
if params:
for key, value in params.items():
body += '--%s\r\n' % boundary
body += 'Content-Disposition: form-data, name=%s\r\n' % key
body += '\r\n'
body += str(value)
body += '\r\n'
body += '--%s\r\n' % boundary
body += 'Content-Disposition: form-data; name=%s; filename=%s\r\n' % (
file_field, media.name)
body += 'Content-Type: application/octet-stream\r\n'
body += '\r\n'
body += media.read()
body += '\r\n--%s--\r\n' % boundary
client = oauth1.Client(
self._consumer_key, client_secret=self._consumer_secret,
resource_owner_key=self._token_key,
resource_owner_secret=self._token_secret, encoding='utf-8',
decoding='utf-8')
uri = self._make_uri(self._upload_url_base, uri)
headers = {
'Content-Type': 'multipart/form-data; boundary=%s' % boundary,
}
uri, headers, _ = client.sign(uri, http_method='POST', headers=headers)
headers = Headers(dict((k, [v]) for k, v in headers.items()))
body_producer = FileBodyProducer(StringIO(body))
d = self._agent.request('POST', uri, headers, body_producer)
d.addCallback(self._handle_error)
d.addCallback(self._parse_response)
return d
示例5: _make_request
# 需要导入模块: from twisted.web.http_headers import Headers [as 别名]
# 或者: from twisted.web.http_headers.Headers import items [as 别名]
def _make_request(self, method, uri, body_parameters=None):
headers = {}
body = None
if body_parameters is not None:
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
body = urlencode(body_parameters)
client = oauth1.Client(
self._consumer_key, client_secret=self._consumer_secret,
resource_owner_key=self._token_key,
resource_owner_secret=self._token_secret, encoding='utf-8',
decoding='utf-8')
uri, headers, body = client.sign(
uri, http_method=method, headers=headers, body=body)
headers = Headers(dict((k, [v]) for k, v in headers.items()))
body_producer = None
if body is not None:
body_producer = FileBodyProducer(StringIO(body))
d = self._agent.request(method, uri, headers, body_producer)
return d.addCallback(self._handle_error)