Python: POST XML over HTTP

Short post to remind myself how to do HTTP requests using python, really easy stuff that I quickly forget.

Straight forward example using httplib and urllib from python. In the example I just perform some kind of login request retrieve the session id and then other request sending XML data using POST.

Code! Feel free to send improvements

  1.  
  2. import httplib, urllib
  3. import re
  4.  
  5. params = urllib.urlencode({‘q’: ‘login’, ‘email’: ‘juasjuas@lol.com’, ‘pwd’: ‘lala’})
  6.  
  7. conn = httplib.HTTPConnection("my.lolizator.com:80")
  8. conn.request("GET", "/cmd.php?"+params)
  9. response = conn.getresponse()
  10. print response.status, response.reason
  11.  
  12. #data = response.read()
  13. #print data
  14.  
  15. cookies = response.getheader("set-cookie")
  16. m = re.search(‘.*PHPSESSID=([a-zA-Z0-9]+);.*’, cookies)
  17. session = m.group(1)
  18. print "session %s" % session
  19. XML=‘<tag1 name="testgroup"><tag2 id="12" type="3"><tag3 id="1">aaa</tag3></tag2></tag1>’
  20. params = urllib.urlencode({‘q’: ’set’})
  21. headers = { "Cookie": "PHPSESSID=" + session, "Content-type": "text/xml",
  22.             "Content-Length": "%d" % len(XML)}
  23. conn.request("POST", "/cmd.php?"+params, "", headers)
  24. conn.send(XML)
  25. response = conn.getresponse()
  26. print response.status, response.reason
  27.  
  28. print response.read()
  29. conn.close()
  30.  
  31.  
, ,
Trackback

no comment untill now

Add your comment now