1
def test(request):
    u="username"
    p= 'password'
    url='url'
    req = urllib2.Request(url)
    base64string = base64.encodestring('%s:%s' % (u , p )).replace('\n', '')
    d = {}
    req.add_data(d)  //while authentication i dont send any data, but it is treated as POST request only if data is sent, so sending empty data
    req.add_header("Authorization", "Basic %s" % base64string) 
    req.add_header('Accept', 'application/json')  
    result = urllib2.urlopen(req)

    print result
    return HttpResponse(result) 

在浏览器中,响应为 (bcoz of HttpResponse)

{"token":"abcdef"}  //means correct

但是命令“打印结果”给出的是这样的:

<addinfourl at 140585554707088 whose fp = <socket._fileobject object at 0x7fdca009e5d0>>

所以,我无法对该变量进行任何操作。我想要的只是字符串中的“令牌”值。我试过 :

type(result) // error
json.dumps(result)  //error 
json.loads(result)  //error 
token = result.token     //error
4

1 回答 1

1

你应该改用

result.read()

result是结果对象。

于 2012-06-30T19:31:12.870 回答