Python urllib.parse.request() Examples

The following are 7 code examples of urllib.parse.request(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib.parse , or try the search function .
Example #1
Source File: AGOLRouteHelper.py    From public-transit-tools with Apache License 2.0 6 votes vote down vote up
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 
Example #2
Source File: sdk.py    From sa-sdk-python with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: AGOLRouteHelper.py    From public-transit-tools with Apache License 2.0 5 votes vote down vote up
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 
Example #4
Source File: hmmer.py    From ssbio with MIT License 5 votes vote down vote up
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'] 
Example #5
Source File: aliyun.py    From letsencrypt-aliyun-dns-manual-hook with MIT License 5 votes vote down vote up
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) 
Example #6
Source File: sdk.py    From sa-sdk-python with Apache License 2.0 5 votes vote down vote up
def _do_request(self, data):
        """
        使用 urllib 发送数据给服务器,如果发生错误会抛出异常。
        """
        encoded_data = urllib.urlencode(data).encode('utf8')
        try:
            request = urllib2.Request(self._url_prefix, encoded_data)

            if self._request_timeout is not None:
                urllib2.urlopen(request, timeout=self._request_timeout)
            else:
                urllib2.urlopen(request)
        except urllib2.HTTPError as e:
            raise SensorsAnalyticsNetworkException(e)
        return True 
Example #7
Source File: ImgurUpload.py    From RMS with GNU General Public License v3.0 5 votes vote down vote up
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