processing XML files in Python and sending them in soap

Source: Internet
Author: User
Tags soap wrapper

The new requirements of the processing of data need to have a payment operation, the function test is to send XML with SOAPUI, but because that XML needs more data and more trouble so want to make automation, tinkering with a week finally succeeded Quq not easy

Record the pits and points that were encountered when doing

1. Data acquisition

Spelling XML to query multiple forms from Oracle, the first is to write a few def each corresponding to a table of SQL

Because in the middle there will be some of the results of the table is the next value, at the beginning to write directly to the file

The result is that it is not available in real time because of the pyc in execution.

Change to return, the result of the final output of the four tables is in a return

Write very novice, feel very cumbersome, but temporarily did not think of a better method Quq

With the Cx_oracle method, the beginning of the database are not even up and down instantclient also not, and finally installed the oracle_x64 full version

But this pit doesn't know if it's because the company's computer was loaded with the x86 version of Oracle.

2. Spelling XML

There are several pits in this.

1) There is a <! in XML [cdata[. ...].] > Need to modify the fields are inside, but the middle of the label is not directly found, first output the middle of this paragraph to a new XML and then modify

2) The results of the new generation of a very easy change, the time to find the character was transcoded, and finally added a Htmlparser library, but with the method of Xml.etree.ElementTree with the way to save or be transcoded

Finally still use a stupid method is the output of the XML re-transcoding Orz, I hope to look back to this article when you can use normal thinking to solve this embarrassing problem?

transcoding code

Html_parser == Html_parser.unescape (All_the_text)

3) was transcoded at the same time also found that the XML above the xmlns into a ns0 check the discovery also need to register their own address, with or xml.etree.ElementTree

Code

Et.register_namespace ('ws'"url") et.register_ Namespace ('soapenv'"url")

PS This paragraph also left a problem where the total number of two tags is not fixed, they are under a large label

If there are fewer or more than the number in the template, you should delete or add the corresponding large tags and the following (the big tag has these two parameters and an empty label)

At present only adds a judgment hint, to the label append and the delete has not explored successfully =-= delay

3. Send Soap

This is not the intention of Baidu to a big guy's method, the basic is to move over to add a bit of their own configuration it Quq

Print Code

#Coding=utf-8ImportHtmlparserhtml_parser=Htmlparser.htmlparser ()ImportXml.dom.minidom as DMdefprint_decorator (argument):defreal_decorator (function):defWrapper (*args, * *Kwargs):Print '-'* 10 +'Start of'+ argument +'-'* 10function (*args, * *Kwargs)Print '-'* 10 +'End of'+ argument +'-'* 10returnwrapperreturnReal_decorator@print_decorator ('Request')defprint_request (Request):Print ' '. Join ((Request.method, Request.path_url)) print_headers (request.headers)Print    ifrequest.body:print_xml (request.body)defprint_headers (headers): forKvinchHeaders.items ():Print ':'. Join ((k, V)) @print_decorator ('Response')defPrint_response (response):PrintResponse.status_codePrintprint_headers (response.headers)#Print Print_xml (Html_parser.unescape (response.text))XML =dm.parsestring (Html_parser.unescape (response.text))Print '-'* 10 +'Response XML'+'-'* 10PrintXml.toprettyxml ()defPrint_xml (XML_STR):PrintXml_str

Send code

#Coding=utf-8ImportRequests fromPrint_utilsImport*ImportHtmlparserImportOSCWD=Os.path.dirname (OS.GETCWD ()) FilePath= Os.path.abspath (CWD +'/xml')defsoapxml ():#URL    #Copy from SOAP 1.1 bodyXML = filepath+"/name.xml"Fileread=Open (XML) Html_parser=Htmlparser.htmlparser ()Try: All_the_text_file=Fileread.read ()#Print All_the_text_fileNew_cont_file =Html_parser.unescape (all_the_text_file)#Print New_cont_file    finally: Fileread.close () request_body=new_cont_file request_headers= {'Host':'URL',                   'Content-type':'text/xml; Charset=utf-8'} response= Requests.post ('URL', the data=request_body, headers=request_headers) s=requests.session () s.keep_alive=False#Requests.adapters.DEFAULT_RETRIES = 5Request =response.request print_request (Request) Print_response (response)

Response's XML was also transcoded at first, adding a dm.parsestring method to the Xml.dom.minidom library.

Did not expect to send soap so simple, Baidu time also see useful URLLIB2 Library to execute, but this method succeeded did not study delay+1 ...

There's a pit in the middle. Send always error, logs are hash errors (in our XML there is a number of fields to spell hash)

Later found that the environment and services may be hung up and restarted on the success _ (: З"∠) _ Long breath ah.

It's going to be a week.

processing XML files in Python and sending them in soap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.