python urllib2请求会话

时间:2014-03-19 15:32:25

标签: python linux

如何根据此脚本将身份验证设置为会话,以便其他网址不需要重新验证?

import os
import subprocess
import sys
import urllib2
import base64
import json
import requests

def proc():
    request = urllib2.Request("https://192.168.174.163:9443/restapi/server/api/v2/cluster")
    base64string = base64.encodestring('%s:%s' % ("admin", "admin")).replace('\n', '')
    request.add_header("Authorization", "Basic %s" % base64string)
    result = urllib2.urlopen(request)
    cookie = result.headers.get('Set-Cookie')
    data = result.read()
    print data
    #convert json data to python dictionary
    json_decoded = json.loads(data)
    print json_decoded
    #cluster name
    print json_decoded["name"]


def main():
    proc()


if __name__ == "__main__":
    main()

0 个答案:

没有答案