Skip to content

Instantly share code, notes, and snippets.

@klattimer
Created March 22, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klattimer/461dc46771920fdf025f to your computer and use it in GitHub Desktop.
Save klattimer/461dc46771920fdf025f to your computer and use it in GitHub Desktop.
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