python urllib2 发起http请求post

使用urllib2发起post请求

def GetCsspToken():
    data = json.dumps({"userName":"wenbin", "password":"passwd"})
    try:
        req  = urllib2.Request('http:9.123.127.9/api/system/login', data, {'Content-Type':'application/json'})
        f = urllib2.urlopen(req)
        response = f.read()
        f.close()
    except urllib2.URLError, e:
        print e
    try:
        return json.loads(response)["entity"]["tokenId"]
    except:
        return ""

  

原文地址:https://www.cnblogs.com/juandx/p/5442734.html