路由器get_rand_key.cgi返回用於後續AES加密的隨機數,該返回值是字典。json
以下代碼,瀏覽器
print pagetext返回字典{"rand_key":"c9d8b128f26058c5a684a212100bba0204beaf1795d227da4601869dd83045cd"}dom
print pagetext['rand_key']提示錯誤TypeError: string indices must be integers, not str加密
import urlparse import urllib import urllib2 import string import json randomkey_url = 'http://192.168.0.1/router/get_rand_key.cgi' request = urllib2.Request(randomkey_url) response = urllib2.urlopen(request) pagetext = response1.read()
print pagetext print pagetext['rand_key']
解決方法:url
在瀏覽器調試中看到返回的json,修改代碼以下spa
import urlparse import urllib import urllib2 import string import json randomkey_url = 'http://192.168.0.1/router/get_rand_key.cgi' request = urllib2.Request(randomkey_url) response = urllib2.urlopen(request) pagetext = json.load(response) print pagetext print pagetext['rand_key']
執行結果調試
{u'rand_key': u'260efad1f2c85451a60c09ee96e11f6c754dade635a10b43c681df39fddca5e0'}
260efad1f2c85451a60c09ee96e11f6c754dade635a10b43c681df39fddca5e0code