SkE/Methods/example: example1.py

File example1.py, 699 bytes (added by husak, 6 months ago)
Line 
1#!/usr/bin/python
2
3import urllib2, base64
4import simplejson
5
6url = 'http://localhost/auth/run.cgi/wordlist?corpname=bnc;wlattr=word;wlminfreq=5;wlmaxitems=100;wlpat=test.*;format=json'
7
8usr = '<username>'
9passwd = '<password>'
10
11request = urllib2.Request(url)
12
13# authentication
14base64string = base64.encodestring('%s:%s' % (usr, passwd))[:-1]
15request.add_header("Authorization", "Basic %s" % base64string)
16
17# json data receiving
18file = urllib2.urlopen(request)
19data = file.read()
20file.close()
21
22# now, in the 'data' variable, there is a json string that can be parsed
23# for json syntax (e.g. by simplejson)
24
25json_obj = simplejson.loads(data)
26print simplejson.dumps(json_obj, sort_keys=True, indent=3)