Flask:会话数据已设置但无法检索

时间:2013-04-14 17:00:09

标签: python session curl flask session-cookies

我可以设置会话数据 / JSON“

me@air9:28:18 ⮀ ~ ⮀ curl -v -X POST http://127.0.0.1:5000 -d '{"email": "user@email.com", "password": "p"}' -H "Content-Type: application/json"
* About to connect() to 127.0.0.1 port 5000 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.27.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Type: application/json
> Content-Length: 44
>
* upload completely sent off: 44 out of 44 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 9
< Set-Cookie: session="DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou"; Path=/; HttpOnly
< Server: Werkzeug/0.8.3 Python/2.7.2
< Date: Sun, 14 Apr 2013 16:28:32 GMT
<
* Closing connection #0
"success"%

以下是检索会话数据的代码

    try:
        print session
        user = load_token(session['USER_TOKEN'])
    except ValueError:
        return redirect(url_for('/'))
    except:
        print repr(sys.exc_info())

当我用curl点击以下内容时

curl -v -b "session='DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou'" http://127.0.0.1:5000/users/uuid/transactions

服务器日志打印

<SecureCookieSession {}>
(<type 'exceptions.KeyError'>, KeyError('USER_TOKEN',), <traceback object at 0x109994248>)

我做错了什么?

如何恢复会话数据?

1 个答案:

答案 0 :(得分:3)

我明白了。我所要做的就是在第一次登录时将会话cookie保存在文件中

curl -v -X POST http://127.0.0.1:5000 -d '{"email": "user@email.com", "password": "p"}' -H "Content-Type: application/json" -c cookies.txt* About to connect() to 127.0.0.1 port 5000 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.27.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Type: application/json
> Content-Length: 44
>
* upload completely sent off: 44 out of 44 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 9
* Added cookie session=""DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou"" for domain 127.0.0.1, path /, expire 0
< Set-Cookie: session="DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou"; Path=/; HttpOnly
< Server: Werkzeug/0.8.3 Python/2.7.2
< Date: Sun, 14 Apr 2013 17:02:00 GMT
<
* Closing connection #0
"success"%

查看Added cookie session

中保存Cookie数据的cookies.txt
$cat cookies.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_127.0.0.1 FALSE   /   FALSE   0   session "DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou"

现在,当我将cookie数据发送到服务器

curl -v -b cookies.txt http://127.0.0.1:5000/users/uuid/transactions # see I am sending the file with -b option

我可以看到服务器接收会话数据

<SecureCookieSession {'USER_TOKEN': '.eJwFwcERwCAIBMBeeIcZQUSsJZMHJ9p_Cdl9CTZHaTeOO50NmZwSyqjs2O56SumhC8ipJmvmuqMpLCTDbaPs9KH0_fiBFCE.tp44K2wvqEdDhyFBl9qCbrZUfsE'}>
data: [u'b475d234-8f76-4baa-a182-bda3bc662ed2', u'fbb1ed0197a9f502b481a864cbd4e352']