Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
I wrote this because I wanted to get the JSON data from OpenWRT's status page and use it elsewhere. Fill out the user name and password, sorry it's a bit hacky but I'm rolling it into other stuff and this is a minimum implementation.
import requests
import re
import urllib2
url = 'http://openwrt/cgi-bin/luci/'
data = {'luci_username': 'root', 'luci_password': 'password'}
r = requests.post(url, data=data, allow_redirects=True)
cookie = r.request.headers['Cookie']
m = re.search('sysauth=(.*)', cookie, re.IGNORECASE)
sysauth = m.group(1)
m = re.search('stok=(.*?)/', r.content, re.IGNORECASE)
stok = m.group(1)
statusurl = url+';stok=' + stok + '?status=1&_=0.0121'
r = urllib2.Request(statusurl)
r.add_header('Cookie', 'sysauth='+sysauth)
resp = urllib2.urlopen(r)
print resp.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment