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
-
-
import httplib, urllib
-
import re
-
-
params = urllib.urlencode({‘q’: ‘login’, ‘email’: ‘juasjuas@lol.com’, ‘pwd’: ‘lala’})
-
-
conn = httplib.HTTPConnection("my.lolizator.com:80")
-
conn.request("GET", "/cmd.php?"+params)
-
response = conn.getresponse()
-
print response.status, response.reason
-
-
#data = response.read()
-
#print data
-
-
cookies = response.getheader("set-cookie")
-
m = re.search(‘.*PHPSESSID=([a-zA-Z0-9]+);.*’, cookies)
-
session = m.group(1)
-
print "session %s" % session
-
XML=‘<tag1 name="testgroup"><tag2 id="12" type="3"><tag3 id="1">aaa</tag3></tag2></tag1>’
-
params = urllib.urlencode({‘q’: ’set’})
-
headers = { "Cookie": "PHPSESSID=" + session, "Content-type": "text/xml",
-
"Content-Length": "%d" % len(XML)}
-
conn.request("POST", "/cmd.php?"+params, "", headers)
-
conn.send(XML)
-
response = conn.getresponse()
-
print response.status, response.reason
-
-
print response.read()
-
conn.close()
-
-
Linux, Python, windows
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.
-
-
import twitter
-
import sys
-
-
if len(sys.argv) != 2:
-
print "Provide a Twitter Username as Argument"
-
exit(-1)
-
-
api = twitter.Api()
-
st = api.GetUserTimeline(sys.argv[1])
-
sum = 0.0
-
for s in st:
-
sum += (140.0 – len(s.text.encode("utf-8")))/140.0
-
-
print "%s internal fragmentation is %.2f%s" % (sys.argv[1], round(sum / len(st) * 100, 2), "%")
-
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%
Linux, open source tools, Python, Software Engineering, Twitter, windows
A few post ago I wrote about integrating SQUID and Active Directory in order to allow/deny users to access specific webpages depeding on the groups a user belongs.
The windows package of Squid comes with several external programs which can be used as external ACLs which allow you to query the local Active Directory in order to obtain access or not. The one dealing with users and groups is called mswin_check_ad_group.exe which, as all the external ACLs, reads the standard input looking for a user and a group and return whether the user belongs to the given group.
This is fine and pretty straight forward it has a PROBLEM, it only works with Groups with scope set to “Domain Local”; which turn into a drawback when your users belong to Groups with Global Scope. I haven’t found any documentation explaining how to achive this so I have created a simple external ACL to peform this task in python.
You only need to download pywin32 and the active directory plugin for python. After installing just use the following code, which will return OK IFF the user belongs to the given group (non matter which scope):
Read the rest of this entry
Active Directory, Python, SQUID, windows