Archive for June 3rd, 2009

I meant to write this up over a week ago now, basically the need arose for one of my Python scripts to use HTTP Basic authentication when grabbing the output from a URL.

An example script can be seen below:

Quick description, the script will attempt to connect to a URL and read the data supplied by the webserver, if a HTTP 401 error is returned (Authentication required) the script will then go on to attempt to authenticate using the credentials supplied.

Printing out to the console at each point.

Subversion: http://svn.saiweb.co.uk/branches/python/urllib2_httpbasic_auth.py

Highlighted source (at the time of writing)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
"""
    Author: David Busby (http://saiweb.co.uk)
    Program: Python HTTP Basic Auth Exa
    Copyright: David Busby 2009. All rights reserved.
    License: http://creativecommons.org/licenses/by-sa/2.0/uk/
"""


import urllib2, base64

""" URL List """
urls = {
               0:{"url":"www.saiweb.co.uk/some/fictional/auth/area","user":"someuser","pass":"somepass"}
}

def main():
   ulen = len(urls)
   for i in range(0,ulen):
       url = "http://%s" % (urls[i]["url"])
       req = urllib2.Request(url)
       try:
           res = urllib2.urlopen(req)
           headers = res.info().headers
           data = res.read()
       except IOError, e:
            if hasattr(e, 'reason'):
                err = "%s ERROR(%s)" % (urls[i]["url"],e.reason)
                print err
            elif hasattr(e, 'code'):
                if e.code != 401:
                    err = "%s ERROR(%s)" % (urls[i]["url"],e.code)
                    print err
                #401 = auth required error
                elif e.code == 401:
                    base64string = base64.encodestring('%s:%s' % (urls[i]["user"], urls[i]["pass"]))[:-1]
                    authheader =  "Basic %s" % base64string
                    req.add_header("Authorization", authheader)
                    try:
                        res = urllib2.urlopen(req)
                        headers = res.info().headers
                        data = res.read()
                    except IOError, e:
                        if hasattr(e, 'reason'):
                            err = "%s:%s@%s ERROR(%s)" % (urls[i]["user"],urls[i]["pass"],urls[i]["url"],e.reason)
                            print err
                        elif hasattr(e, 'code'):
                            err = "%s:%s@%s ERROR(%s)" % (urls[i]["user"],urls[i]["pass"],urls[i]["url"],e.code)
                            print err
                    else:
                        err = "%s query complete" % (urls[i]["url"])
                        print err
       else:
            err = "%s query complete" % (urls[i]["url"])
            print err
                       
if __name__ == "__main__":
    main()

NOTES: This script does not check the authentication type, it always assumes it is HTTP BASIC, HTTP DIGEST for example is not compatible with this script, though there is no reason why you can not get the Auth type form the headers returned by the server and write in a Digest auth method.

Tags: , , , ,

Comments No Comments »

As most have no doubt noticed the plugin is delayed … _again_

Too many projects, too few hours in the day.

Froomi – All in one HTTP Server, Search Engine, Pseudo Streaming Media server
(Might add transcoding)

This project is up on google code: http://code.google.com/p/froomi/

So far the HTTP module is basically complete and already has GZIP compression support for browsers that send the appropriate header to indicate they support it.

I will being working on the Pseudo streaming component shortly, to recap this is in an Alpha state at the moment, I will be putting the server live on http://www.froomi.com shortly once the Pseudo streaming component is complete.

Flowplayer for wordpress, is still in development the code base has been completely re-written as per the previews you can now see the meta boxes in action, along with the license detection, I think I am going to cut a couple of features and just get the next release out the door (yes RTMP will still be in this release!).

Contributions, some people have shown an interest in contributing to these projects, contributions are very welcome and will carry the appropriate credits in the source code and license, I would go so far as to encourage contributions from developers as this will actually speed up releases for these projects.

So feel free if to contact me.

Contributor Requirements:

  • Familiarity with the programming language used in the project you are contributing to (PHP,Python,etc..)
  • Working knowledge of creating diff patches (not required bug a huge bonus)
  • Ability to document your changes in English, sorry this is a requirements
  • Skype / MSN / Ventrilo client

o

Tags: , , ,

Comments No Comments »