[英]Flask: session data is set but can't retrieved
I can set session data /json" 我可以设置会话数据/ 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"%
Here is the code that retrieves session data 以下是检索会话数据的代码
try:
print session
user = load_token(session['USER_TOKEN'])
except ValueError:
return redirect(url_for('/'))
except:
print repr(sys.exc_info())
When I hit the following with curl 当我用卷曲击中下面的时候
curl -v -b "session='DRRrrffY/WGHU80TJ++KY5UaoYU=?USER_TOKEN=UycuZUp3RndjRVJ3Q0FJQk1CZWVJY1pRVVNzSlpNSEo5cF9DZGw5Q1RaSGFUZU9PNTBObVp3U3lxanMyTzU2U3VtaEM4aXBKbXZtdXFNcExDVERiYVBzOUtIMF9maUJGQ0UudHA0NEsyd3ZxRWREaHlGQmw5cUNiclpVZnNFJwpwMQou'" http://127.0.0.1:5000/users/uuid/transactions
The server log prints 服务器日志打印
<SecureCookieSession {}>
(<type 'exceptions.KeyError'>, KeyError('USER_TOKEN',), <traceback object at 0x109994248>)
What is that I am doing wrong? 我做错了什么?
How can I get the session data back? 如何获取会话数据?
I figured it out. 我想到了。 all I had to do is save the session cookie in a file when first time logging in
我所要做的就是在第一次登录时将会话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"%
see the Added cookie session
which saves cookie data in cookies.txt
请参阅已
Added cookie session
,该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"
Now, when I send the cookie data to server 现在,当我将cookie数据发送到服务器时
curl -v -b cookies.txt http://127.0.0.1:5000/users/uuid/transactions # see I am sending the file with -b option
I can see server receiving the session data 我可以看到服务器接收会话数据
<SecureCookieSession {'USER_TOKEN': '.eJwFwcERwCAIBMBeeIcZQUSsJZMHJ9p_Cdl9CTZHaTeOO50NmZwSyqjs2O56SumhC8ipJmvmuqMpLCTDbaPs9KH0_fiBFCE.tp44K2wvqEdDhyFBl9qCbrZUfsE'}>
data: [u'b475d234-8f76-4baa-a182-bda3bc662ed2', u'fbb1ed0197a9f502b481a864cbd4e352']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.