Java to Python

Aleksandar Radulovic alex at a13x.net
Sat Feb 7 12:09:42 EST 2009


Hi,

This looks like a perfect job for httplib and urllib2 modules.

On Sat, Feb 7, 2009 at 4:49 PM, zaheer agadi <zaheer.agadi at gmail.com> wrote:
> Hi Thanks for replying ..
> I am actually looking for the pure Python options
>
> Are there any equivalent clasees  for the following
>
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.HttpException;
> import
> org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
> import org.apache.commons.httpclient.methods.GetMethod;
> import org.apache.commons.httpclient.protocol.Protocol;
>
>
> Thanks for your help
> -Zaheer
>
> On Sat, Feb 7, 2009 at 9:57 PM, Banibrata Dutta <banibrata.dutta at gmail.com>
> wrote:
>>
>> Jython is not an option ?
>>
>> On Sat, Feb 7, 2009 at 9:54 PM, <zaheer.agadi at gmail.com> wrote:
>>>
>>> Hi
>>>
>>> I have a following class that is written Java and makes use of apache
>>> http client library,I am new to python can any one suggest me a python
>>> equivalent of this following class,
>>>
>>> Thanks ,
>>>
>>> public class Authenticate{
>>>
>>>  private String storageUserName=null;
>>>    private String storagePassword=null;
>>>    private String authorization=null;
>>>    private String identityHostName = null;
>>>    private String identityPortNumber = null;
>>>
>>>    private String accessKey=null;
>>>    private String secretKey=null;
>>>
>>>    public String getStoragePassword() {
>>>        return storagePassword;
>>>    }
>>>
>>>    public void setStoragePassword(String storagePassword) {
>>>        this.storagePassword = storagePassword;
>>>    }
>>>
>>>    public String getStorageUserName() {
>>>        return storageUserName;
>>>    }
>>>
>>>    public void setStorageUserName(String storageUserName) {
>>>        this.storageUserName = storageUserName;
>>>    }
>>>
>>>    public String getIdentityHostName() {
>>>        return identityHostName;
>>>    }
>>>
>>>    public void setIdentityHostName(String identityHostName) {
>>>        this.identityHostName = identityHostName;
>>>    }
>>>
>>>    public String getIdentityPortNumber() {
>>>        return identityPortNumber;
>>>    }
>>>
>>>    public void setIdentityPortNumber(String identityPortNumber) {
>>>        this.identityPortNumber = identityPortNumber;
>>>    }
>>>
>>>    public String getAccessKey() {
>>>        return accessKey;
>>>    }
>>>
>>>    public void setAccessKey(String accessKey) {
>>>        this.accessKey = accessKey;
>>>    }
>>>
>>>    public String getSecretKey() {
>>>        return secretKey;
>>>    }
>>>
>>>    public void setSecretKey(String secretKey) {
>>>        this.secretKey = secretKey;
>>>    }
>>>
>>>
>>>     /**
>>>     * <p>Convenience string for Base 64 encoding.</p>
>>>     */
>>>    private static final String BASE64_CHARS =
>>>            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
>>>            "abcdefghijklmnopqrstuvwxyz" +
>>>            "0123456789+/";
>>>
>>>    /**
>>>     * <p>Encode the specified credentials into a String as required
>>> by
>>>     * HTTP Basic Authentication (<a href="http://www.ietf.org/rfc/
>>> rfc2617.txt">RFC 2617</a>).</p>
>>>     *
>>>     * @param username Username to be encoded
>>>     * @param password Password to be encoded
>>>     * @return String string containing encoded username and password.
>>>     */
>>>    public String encodeCredentialsBasic(String username, String
>>> password) {
>>>        String encode = username + ":" + password;
>>>        int paddingCount = (3 - (encode.length() % 3)) % 3;
>>>        encode += "\0\0".substring(0, paddingCount);
>>>        StringBuilder encoded = new StringBuilder();
>>>
>>>        for (int i = 0; i < encode.length(); i += 3) {
>>>        }
>>>        return encoded.toString();
>>>    }
>>>
>>>    public void fetchDetails(){
>>>        HttpClient client=new HttpClient();
>>>        //reqDetails = new RequestDetails();
>>>        //String identityURL=MessageUtil.getMessage
>>> ("IDENTITY_INSTANCE");
>>>        //int portNumber=Integer.parseInt(MessageUtil.getMessage
>>> ("IDENTITY_PORT"));
>>>        authorization="Basic " + encodeCredentialsBasic
>>> (storageUserName, storagePassword);
>>>        String url="https://"+identityHostName+
>>>                ":"+identityPortNumber+"/test/ndcsd2/persons/"+UserName
>>> +"/attributes/";
>>>
>>>        Protocol https=null;
>>>        //try {
>>>            https = new Protocol("https", new
>>> EasySSLProtocolSocketFactory(), Integer.parseInt(identityPortNumber));
>>>        /*} catch (GeneralSecurityException ex) {
>>>            Logger.getLogger(Authenticate.class.getName()).log
>>> (Level.SEVERE, null, ex);
>>>        } catch (IOException ex) {
>>>            Logger.getLogger(Authenticate.class.getName()).log
>>> (Level.SEVERE, null, ex);
>>>        }*/
>>>        Protocol.registerProtocol("https", https);
>>>        GetMethod method=new GetMethod(url);
>>>        method.setRequestHeader("Authorization",authorization);
>>>        method.setRequestHeader("Accept","application/xml");
>>>        try {
>>>            int responseCode=client.executeMethod(method);
>>>            if(responseCode==200){
>>>                InputStream is=method.getResponseBodyAsStream();
>>>                BufferedReader bis=new BufferedReader(new
>>> InputStreamReader(is));
>>>                String temp=null,sKey=null, aKey=null;
>>>                String accessKeySearchString="AccessKey</
>>> Name><Value>";
>>>                String secretKeySearchString="SecretKey</
>>> Name><Value>";
>>>                int searchStringLength=0;
>>>                while((temp=bis.readLine())!=null){
>>>                    if(temp.indexOf(accessKeySearchString)!=-1){
>>>                        int beginIndex=temp.indexOf
>>> (accessKeySearchString);
>>>                        searchStringLength=accessKeySearchString.length
>>> ();
>>>                        int endIndex=temp.indexOf("</
>>> Value>",beginIndex);
>>>                        aKey=temp.substring(beginIndex
>>> +searchStringLength,endIndex);
>>>                    }
>>>                    if(temp.indexOf(secretKeySearchString)!=-1){
>>>                        int beginIndex=temp.indexOf
>>> (secretKeySearchString);
>>>                        searchStringLength=secretKeySearchString.length
>>> ();
>>>                        int endIndex=temp.indexOf("</
>>> Value>",beginIndex);
>>>                        sKey=temp.substring(beginIndex
>>> +searchStringLength,endIndex);
>>>                    }
>>>                }
>>>                setSecretKey(sKey);
>>>                setAccessKey(aKey);
>>>            } else {
>>>                System.out.println("Not able to get the credentials.
>>> Returned : " + responseCode + " response code!!!");
>>>            }
>>>        } catch (IOException ex) {
>>>            Logger.getLogger(Authenticate.class.getName()).log
>>> (Level.SEVERE, null, ex);
>>>        }
>>>    }
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>>
>> --
>> regards,
>> Banibrata
>> http://www.linkedin.com/in/bdutta
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



-- 
a lex 13 x
http://www.a13x.info



More information about the Python-list mailing list