How to set/update value in a xml file using requests in python

dieter dieter at handshake.de
Wed Feb 7 02:35:37 EST 2018


Sum J <sjsumitj at gmail.com> writes:
> My xml file is located in local network : http://192.168.43.109/DevMgmt/NetAppsDyn.xml
>
> Below is a part content of above xml I want to update :
>
>     <nadyn:ProxyConfig>
>     <dd:ResourceURI/>
>     <dd:Port/>
>     <dd:ProxySupport>off</dd:ProxySupport>
>     </nadyn:ProxyConfig>
>
> I want to set value for 'ResourceUI' and 'Port' field in above xml.
> I have used below code :
>
>      data = {
>               'ResourceURI':'web-proxy.xxx.yy.com',
>               'Port':8080
>             }
>
>     URL = 'http://192.168.75.165/DevMgmt/NetAppsDyn.xml'
>
>     # content type
>     head = {'Content-type': 'text/xml'}
>     # sending get request
>     gr= requests.get(url=URL)
>     print gr
>
>     # sending put request
>     r = requests.put(url=URL, data=data,headers=head)
>     print r.status_code
>     # extracting response text
>     output_xml = r.text
>     print("The op xml is:%s" % output_xml)
>
> Issue : The fields are not getting updated in xml using put request. I am able to see the response for get (request) , but for put request it is throwing errror code : 301 , resource has been moved permanently.

This may not be a Python question.

You are sending your data
("{'ResourceURI':'web-proxy.xxx.yy.com', 'Port':8080})
to the web service under "http://192.168.75.165/DevMgmt/NetAppsDyn.xml".
Apparently, this web service does not interpret the data in the way you
expect.

Note, that you do not send an XML document to "NetAppsDyn.xml"
(despite your `Content-type="text/xml"`). The typical (WevDAV)
"put" would require that "data" contains a complete replacement
for the object to be replaced, not just some modifications
(of course, the object might have a specialized "put" which could
support partial updates -- but apparently, in your current
situation, this is either not the case or the "put" involved
expects to get the replacement information in a different way).

I would approach your task as follows:

 1. Fetch the original XML

 2. Use one of Python's XML libraries (I would use "lxml")
    to create the modified XML

 3. "put" the modified XML to the server




More information about the Python-list mailing list