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%

, , , , ,

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

, , ,

In this last post about NSIS I am going to describe how to use some of the most useful plugins which will allow you create a pretty decent and featurefull installer for windows.

  • Checking for adminstrator privileges:

userInfo::getAccountType

Pop $0

StrCmp $0 “Admin” +3
MessageBox MB_OK “Debe tener privilegios de administrador para correr este programa de instalación: $0″
Return

StrCmp $0 “Admin” +3

MessageBox MB_OK “You need Administrator privileges: $0″

Return

This chunk of code will pop up a windows with the propoer notification. The StrCmp sentence compares the $0 variable poped from the stack against “Admin” and if they are equal continue the execution with the 3rd line of code, the one below Return.

  • Downloading and Executing, e.g. an external installer:

NSISdl::download /TIMEOUT=30000 “http://www.advansen.com/this/is/a/test/advansen.msi” “$INSTDIR\advansen.msi”

Pop $R0 ;Get the return value

StrCmp $R0 “success” +3

MessageBox MB_OK “Download failed: $R0″

Quit

ExecWait  ’”msiexec” /i “$INSTDIR\advansen.msi” /quiet’

Read the rest of this entry

, ,

After playing for a few days with NSIS I have manage to create a full features installer which perform all the actions I needed. These are the following:

  • Download files
  • Unzip Files to a specific folder
  • Execute external installers
  • Modify Environment Variables

Since NSIS supports plug-ins there are quite a few available which provide extra functionalities such as the unzipping one.

Before going into these extra features, which will be subject of a new post, I prefer writing down how a more complex script is structure, just to keep in to the future.

NSIS is structured in pages (page == panels show each time a “next button” is clicked), most of them are predefined, where you can only change tittle or add extra text in them. Following are the pages definition using the MUI2 interface:

!insertmacro MUI_PAGE_LICENSE “path\to\license.txt”

!insertmacro MUI_PAGE_COMPONENTS

!insertmacro MUI_PAGE_DIRECTORY

!insertmacro MUI_PAGE_INSTFILES

Read the rest of this entry

, ,

Last week I faced a new problem, I had to program a installer for Windows in order to deploy some software we have produced at work. After a quick googling I found NSIS an installer generator, open source and with a simple scripting language.

NSIS comes with a compiler which parses the sentences writting in its own language and produces a pretty decent installer. It also has a look-and-feeling similar to latets Windows versions, so it was the perfect choice!

The scripting language is really simple, we can generate a simple installer with only the next few lines:

outFile “installer.exe”
installDir $DESKTOP
section
userInfo::getAccountType
pop $0
strCmp $0 “Admin” +3
messageBox MB_OK “not admin: $0″
return
messageBox MB_OK “is admin”
setOutPath $INSTDIR
file test.txt
writeUninstaller $INSTDIR\uninstaller.exe
createShortCut “$SMPROGRAMS\new oxtias.lnk” “$INSTDIR\uninstaller.exe”
sectionEnd
section “Uninstall”
delete $INSTDIR\uninstaller.exe
delete $INSTDIR\test.txt
delete “$SMPROGRAMS\new oxtias.lnk”
sectionEnd

OutFile “simpleAdvansenInstaller.exe”

InstallDir $DESKTOP

Section

SetOutPath $INSTDIR

WriteUninstaller $INSTDIR\uninstaller.exe

CreateShortCut “$SMPROGRAMS\simpleAdvansen.lnk” “$INSTDIR\uninstaller.exe”

SectionEnd

Section “Uninstall”

Delete $INSTDIR\uninstaller.exe

Delete $INSTDIR\test.txt

Delete “$SMPROGRAMS\simpleAdvansen.lnk”

SectionEnd

After compiling the script with the tool NSIS package’s comes with, two executables are generated:

simpleAdvansenInstaller.exe

Which will copy the uninstaller.exe to the Installation Dir set up to the Desktop and will add an entry in the Programs Menu for the uninstaller.

uninstaller.exe

Will delete itself and remove the entry in the Programs Menu.

NOTE: NotePad++ highlights the syntax of NSIS as well.

, ,