本文整理匯總了Python中urllib.parse.request方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.request方法的具體用法?Python parse.request怎麽用?Python parse.request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.parse
的用法示例。
在下文中一共展示了parse.request方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: solve_routes
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def solve_routes(tokenstuff, service_params):
# URL to Esri ArcGIS Online asynchronous routing service
service_url = "http://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve"
# Make sure the token isn't about to expire. If it is, wait until it expires and then get a new one.
now = datetime.datetime.now()
tokenexpiretime = datetime.datetime.fromtimestamp(tokenstuff['expires'])
if tokenexpiretime - now < datetime.timedelta(seconds=5):
time.sleep(5)
get_token()
tokenstuff = token
#Execute the request
response, response_time = execute_request(service_url, tokenstuff['token'], tokenstuff['referer'], service_params)
return response
示例2: _do_request
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def _do_request(self, data):
"""
使用 urllib 發送數據給服務器,如果發生錯誤會拋出異常。
response的結果,會返回
"""
encoded_data = urllib.urlencode(data).encode('utf8')
try:
request = urllib2.Request(self._debug_url_prefix, encoded_data)
if not self._debug_write_data: # 說明隻檢查,不真正寫入數據
request.add_header('Dry-Run', 'true')
if self._request_timeout is not None:
response = urllib2.urlopen(request, timeout=self._request_timeout)
else:
response = urllib2.urlopen(request)
except urllib2.HTTPError as e:
return e
return response
示例3: makeHTTPRequest
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def makeHTTPRequest(url, query_params=None, content_coding_token="gzip", referer=None):
"""Makes an HTTP request and returns the JSON response. content_coding_token
must be gzip or identity.
If content_coding_token is identity, response does not need any transformation.
If content_coding_token is gzip, the response special handling before converting to JSON."""
response_dict = {}
if query_params == None:
query_params = {}
if not "f" in query_params:
query_params["f"] = "json"
request = urllib2.Request(url)
if ispy3:
data = urllib.urlencode(query_params)
binary_data = data.encode('utf-8')
request.data = binary_data
else:
request.add_data(urllib.urlencode(query_params))
request.add_header("Accept-Encoding", content_coding_token)
if referer:
request.add_header("Referer", referer)
response = urllib2.urlopen(request)
if content_coding_token == "gzip":
if response.info().get("Content-Encoding") == "gzip":
if ispy3:
# Encoding is complicated in python3
buf = sio.BytesIO(response.read())
response = gzip.GzipFile(fileobj=buf, mode='rb')
reader = codecs.getreader("utf-8")
response = reader(response)
else:
buf = sio.StringIO(response.read())
response = gzip.GzipFile(fileobj=buf)
response_dict = json.load(response)
return response_dict
示例4: manual_get_pfam_annotations
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def manual_get_pfam_annotations(seq, outpath, searchtype='phmmer', force_rerun=False):
"""Retrieve and download PFAM results from the HMMER search tool.
Args:
seq:
outpath:
searchtype:
force_rerun:
Returns:
Todo:
* Document and test!
"""
if op.exists(outpath):
with open(outpath, 'r') as f:
json_results = json.loads(json.load(f))
else:
fseq = '>Seq\n' + seq
if searchtype == 'phmmer':
parameters = {'seqdb': 'pdb', 'seq': fseq}
if searchtype == 'hmmscan':
parameters = {'hmmdb': 'pfam', 'seq': fseq}
enc_params = urllib.urlencode(parameters).encode('utf-8')
request = urllib2.Request('http://www.ebi.ac.uk/Tools/hmmer/search/{}'.format(searchtype), enc_params)
url = (urllib2.urlopen(request).geturl() + '?output=json')
request = str(url)
request_read = urlopen(request).read().decode("utf-8")
with open(outpath, 'w') as f:
json.dump(request_read, f)
json_results = json.loads(request_read)
return json_results['results']['hits']
示例5: __request
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def __request(self, params):
commonParams = {
'Format': 'JSON',
'Version': '2015-01-09',
'SignatureMethod': 'HMAC-SHA1',
'SignatureNonce': self.__getSignatureNonce(),
'SignatureVersion': '1.0',
'AccessKeyId': self.__appid,
'Timestamp': time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
}
# print(commonParams)
# merge all params
finalParams = commonParams.copy()
finalParams.update(params)
# signature
finalParams['Signature'] = self.__signature(finalParams)
self.__logger.info('Signature'+ str(finalParams['Signature']))
# get final url
url = '%s/?%s' % (self.__endpoint, urllib.urlencode(finalParams))
# print(url)
request = urllib2.Request(url)
try:
f = urllib2.urlopen(request)
response = f.read()
self.__logger.info(response.decode('utf-8'))
except urllib2.HTTPError as e:
self.__logger.info(e.read().strip().decode('utf-8'))
raise SystemExit(e)
示例6: imgurUpload
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import request [as 別名]
def imgurUpload(file_path, image_data=None):
""" Upload the given image to Imgur.
Arguments:
file_path: [str] Path to the image file.
Keyword arguments:
image_data: [bytes] Read in image in JPG, PNG, etc. format.
Return:
img_url: [str] URL to the uploaded image.
"""
# Read the image if image data was not given
if image_data is None:
# Open the image in binary mode
f = open(file_path, "rb")
image_data = f.read()
# Encode the image
b64_image = base64.standard_b64encode(image_data)
# Upload the image
headers = {'Authorization': 'Client-ID ' + CLIENT_ID}
data = {'image': b64_image, 'title': 'test'} # create a dictionary.
request = urllib2.Request(url="https://api.imgur.com/3/upload.json",
data=urllib.urlencode(data).encode("utf-8"), headers=headers)
response = urllib2.urlopen(request).read()
# Get URL to image
parse = json.loads(response)
img_url = parse['data']['link']
return img_url