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.  
, ,

As a toy project to play a little bit more with Python and accessing Twitter, I came out with the idea of calculating the Internal Fragmentation of user’s tweet.

To interface with Twitter services I used the Twitter extension located at http://code.google.com/p/python-twitter/, which has a pretty straightforward API.

The script shown below gives you back what average percentage of your last 20 tweets have been wasted.

  1.  
  2. import  twitter
  3. import  sys
  4.  
  5. if len(sys.argv) != 2:
  6.         print   "Provide a Twitter Username as Argument"
  7.         exit(-1)
  8.  
  9. api = twitter.Api()
  10. st = api.GetUserTimeline(sys.argv[1])
  11. sum = 0.0
  12. for s in st:
  13.         sum += (140.0len(s.text.encode("utf-8")))/140.0
  14.  
  15. print "%s internal fragmentation is %.2f%s" % (sys.argv[1], round(sum / len(st) * 100, 2), "%")
  16.  

And now some results:

$ python twinternal.py GabrielGonzalez
GabrielGonzalez internal fragmentation is 39.89%
$ python twinternal.py 48bits
48bits internal fragmentation is 36.79%
$ python twinternal.py reversemode
reversemode internal fragmentation is 38.72%
$ python twinternal.py aramosf
aramosf internal fragmentation is 32.41%

, , , , ,