开发者

Replicate Java into Python

I'm not very well-versed in how the internets work, so I'm not really sure what this java method is doing or how to best replicate it in python. I have tried several different methods including urllibs and sockets, but nothing seems to work. The only time I get a response I end up getting a huge HTML document when the response should only be about 5 lines of xml.

Any help would be greatly appreciate, thanks guys :).

try  {
     开发者_如何学C       URL url = new URL( sPROTOCOL, sHOSTNAME, sPAGENAME );
            URLConnection url_con = url.openConnection();
            url_con.setDoInput(true);
            url_con.setDoOutput(true);
            url_con.setUseCaches (false);
            url_con.setRequestProperty ("content-type", "application/x-www-form-urlencoded");

            String input_xml =  make_XML( sAppID, sAppPassword, sUserID, sPassword );
            if (bDEBUG) {
                    System.out.println( "\nINPUT XML------------------\n" + input_xml );
                    System.out.println( "\nEND INPUT XML--------------\n" );
            }
            BufferedWriter writebuf = new BufferedWriter(new OutputStreamWriter(url_con.getOutputStream()));
            writebuf.write("XMLData=");
            writebuf.write( URLEncoder.encode( input_xml, "UTF-8" ) );  //Java 1.4.x and later
            //writebuf.write( URLEncoder.encode( input_xml ) );  //Java 1.3.1 and earlier
            writebuf.flush();
            writebuf.close();
            writebuf = null;
            HashMap hm = parseResp(url_con);


it looks like it's opening a connection to sHOSTNAME, sending the XML data generated by make_XML (apparently as a single POST parameter called XMLData, so sPROTOCOL must be HTTP), and then processing the response in parseResp.

in python you would use httplib. the final example at http://docs.python.org/library/httplib.html is doing something similar (but is sending three parameters). note that the code you posted is kind of ugly, in that it explicitly writes the POST contents - in python you would just give the XML as a parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜