Skip to content

Instantly share code, notes, and snippets.

@hoest
Created September 6, 2012 11:47
Show Gist options
  • Select an option

  • Save hoest/3655337 to your computer and use it in GitHub Desktop.

Select an option

Save hoest/3655337 to your computer and use it in GitHub Desktop.

Revisions

  1. hoest created this gist Sep 6, 2012.
    33 changes: 33 additions & 0 deletions post.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import httplib
    import xml.dom.minidom

    HOST = "www.domain.nl"
    API_URL = "/api/url"


    def do_request(xml_location):
    """HTTP XML Post request"""
    request = open(xml_location, "r").read()

    webservice = httplib.HTTP(HOST)
    webservice.putrequest("POST", API_URL)
    webservice.putheader("Host", HOST)
    webservice.putheader("User-Agent", "Python post")
    webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
    webservice.putheader("Content-length", "%d" % len(request))
    webservice.endheaders()

    webservice.send(request)

    statuscode, statusmessage, header = webservice.getreply()

    result = webservice.getfile().read()
    resultxml = xml.dom.minidom.parseString(result)

    print statuscode, statusmessage, header
    print resultxml.toprettyxml()

    with open("output-%s" % xml_location, "w") as xmlfile:
    xmlfile.write(resultxml.toprettyxml())

    do_request("xml-request-file.xml")