Python google.appengine.api.urlfetch 模块,PUT 实例源码
我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用google.appengine.api.urlfetch.PUT。
@H_502_22@def __init__(self, host, port=None, strict=False, timeout=None):
from google.appengine.api import urlfetch
self._fetch = urlfetch.fetch
self._method_map = {
'GET': urlfetch.GET,
'POST': urlfetch.POST,
'HEAD': urlfetch.HEAD,
'PUT': urlfetch.PUT,
'DELETE': urlfetch.DELETE,
'PATCH': urlfetch.PATCH,
}
self.host = host
self.port = port
self._method = self._url = None
self._body = ''
self.headers = []
if @H_502_228@not isinstance(timeout, (float, int, long)):
timeout = None
self.timeout = timeout
@H_502_22@def __init__(self, strict=None,
timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None,
context=None):
# net.proto.ProcotolBuffer relies on httplib so importing urlfetch at the
# module level causes a failure on prod. That means the import needs to be
# lazy.
from google.appengine.api import urlfetch
self._fetch = urlfetch.fetch
self._method_map = {
'GET': urlfetch.GET,
}
self.host = host
self.port = port
# With urllib2 in Python 2.6,an object can be passed here.
# The default is set to socket.GLOBAL_DEFAULT_TIMEOUT which is an object.
# We only accept float,int or long values,otherwise it can be
# silently ignored.
if @H_502_228@not isinstance(timeout, long)):
timeout = None
self.timeout = timeout
# Both 'strict' and 'source_address' are ignored.
self._method = self._url = None
self._body = ''
self.headers = []
@H_502_22@def request(self, operation, url, data=None, headers=None):
"""Performs an HTTP call to the server,supports GET,POST,PUT,and
DELETE.
Usage example,perform and HTTP GET on http://www.google.com/:
import atom.http
client = atom.http.HttpClient()
http_response = client.request('GET','http://www.google.com/')
Args:
operation: str The HTTP operation to be performed. This is usually one
of 'GET','POST','PUT',or 'DELETE'
data: filestream,list of parts,or other object which can be converted
to a string. Should be set to None when performing a GET or DELETE.
If data is a file-like object which can be read,this method will
read a chunk of 100K bytes at a time and send them.
If the data is a list of parts to be sent,each part will be
evaluated and sent.
url: The full URL to which the request should be sent. Can be a string
or atom.url.Url.
headers: dict of strings. HTTP headers which should be sent
in the request.
"""
all_headers = self.headers.copy()
if headers:
all_headers.update(headers)
# Construct the full payload.
# Assume that data is None or a string.
data_str = data
if data:
if isinstance(data, list):
# If data is a list of different objects,convert them all to strings
# and join them together.
converted_parts = [__ConvertDataPart(x) for x @H_502_228@in data]
data_str = ''.join(converted_parts)
else:
data_str = __ConvertDataPart(data)
# If the list of headers does not include a Content-Length,attempt to
# calculate it based on the data object.
if data @H_502_228@and 'Content-Length' @H_502_228@not @H_502_228@in all_headers:
all_headers['Content-Length'] = len(data_str)
# Set the content type to the default value if none was set.
if 'Content-Type' @H_502_228@not @H_502_228@in all_headers:
all_headers['Content-Type'] = 'application/atom+xml'
# Lookup the urlfetch operation which corresponds to the desired HTTP verb.
if operation == 'GET':
method = urlfetch.GET
elif operation == 'POST':
method = urlfetch.POST
elif operation == 'PUT':
method = urlfetch.PUT
elif operation == 'DELETE':
method = urlfetch.DELETE
else:
method = None
return HttpResponse(urlfetch.Fetch(url=str(url), payload=data_str,
method=method, headers=all_headers))
@H_502_22@def request(self, headers=all_headers))
@H_502_22@def request(self, headers=None):
"""Performs an HTTP call to the server,and
DELETE.
Usage example,perform and HTTP GET on http://www.google.com/:
import atom.http
client = atom.http.HttpClient()
http_response = client.request('GET','http://www.google.com/')
Args:
operation: str The HTTP operation to be performed. This is usually one
of 'GET',or 'DELETE'
data: filestream,or other object which can be converted
to a string. Should be set to None when performing a GET or DELETE.
If data is a file-like object which can be read,this method will
read a chunk of 100K bytes at a time and send them.
If the data is a list of parts to be sent,each part will be
evaluated and sent.
url: The full URL to which the request should be sent. Can be a string
or atom.url.Url.
headers: dict of strings. HTTP headers which should be sent
in the request.
"""
all_headers = self.headers.copy()
if headers:
all_headers.update(headers)
# Construct the full payload.
# Assume that data is None or a string.
data_str = data
if data:
if isinstance(data, list):
# If data is a list of different objects,convert them all to strings
# and join them together.
converted_parts = [__ConvertDataPart(x) for x @H_502_228@in data]
data_str = ''.join(converted_parts)
else:
data_str = __ConvertDataPart(data)
# If the list of headers does not include a Content-Length,attempt to
# calculate it based on the data object.
if data @H_502_228@and 'Content-Length' @H_502_228@not @H_502_228@in all_headers:
all_headers['Content-Length'] = len(data_str)
# Set the content type to the default value if none was set.
if 'Content-Type' @H_502_228@not @H_502_228@in all_headers:
all_headers['Content-Type'] = 'application/atom+xml'
# Lookup the urlfetch operation which corresponds to the desired HTTP verb.
if operation == 'GET':
method = urlfetch.GET
elif operation == 'POST':
method = urlfetch.POST
elif operation == 'PUT':
method = urlfetch.PUT
elif operation == 'DELETE':
method = urlfetch.DELETE
else:
method = None
return HttpResponse(urlfetch.Fetch(url=str(url),
method=method, headers=all_headers))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。