How to POST an xml element in python

前端 未结 3 1948
说谎
说谎 2021-02-01 08:32

Basically I have this xml element (xml.etree.ElementTree) and I want to POST it to a url. Currently I\'m doing something like

xml_string = xml.etree.ElementTree         


        
3条回答
  •  一个人的身影
    2021-02-01 09:10

    Here is a full example (snippet) for sending post data (xml) to an URL:

    def execQualysAction(username,password,url,request_data):
      import urllib,urrlib2
      xml_output = None 
      try:
        base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')  
        headers = {'X-Requested-With' : 'urllib2','Content-Type': 'application/xml','Authorization': 'Basic %s' % base64string}
        req = urllib2.Request(url=url,data=request_data,headers=headers)
        response = urllib2.urlopen(req,timeout=int(TIMEOUT))
        xml_output = response.read()
        if args.verbose>1:
          print "Result of executing action request",request_data,"is:",xml_output
      except:
        xml_output = ''
        traceback.print_exc(file=sys.stdout)
        print '-'*60
    

    finally:

    return xml_output
    

提交回复
热议问题