Python urllib.parse 模块,request() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用urllib.parse.request()

项目:ssbio    作者:SBRG    | 项目源码 | 文件源码
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']
项目:api_stuff    作者:tgebhart    | 项目源码 | 文件源码
def superSecureRequest(self, host, path, url_params=None):
        """Prepares OAuth authentication and sends the request to the API.
        Args:
            host (str): The domain host of the API.
            path (str): The path of the API after the domain.
            url_params (dict): An optional set of query parameters in the request.
        Returns:
            dict: The JSON response from the request.
        Raises:
            urllib2.HTTPError: An error occurs from the HTTP request.
        """
        url_params = url_params or {}
        url = 'https://{0}{1}?'.format(host, urllib.quote(path.encode('utf8')))

        consumer = oauth2.Consumer(self.oath_consumer_key, self.oath_consumer_secret)
        oauth_request = oauth2.Request(
            method="GET", url=url, parameters=url_params)

        oauth_request.update(
            {
                'oauth_nonce': oauth2.generate_nonce(),
                'oauth_timestamp': oauth2.generate_timestamp(),
                'oauth_token': self.oath_token,
                'oauth_consumer_key': self.oath_consumer_key
            }
        )
        token = oauth2.Token(self.oath_token, self.oath_token_secret)
        oauth_request.sign_request(
            oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
        signed_url = oauth_request.to_url()

        conn = urllib2.urlopen(signed_url, None)
        try:
            response = json.loads(conn.read().decode())
        finally:
            conn.close()

        return response