2016-07-05 5 views
0

JSON文字列を値として使用する場合、PythonのSimpleCookieを使用していくつかの問題が発生しました。Python SimpleCookieとJSONの値

In [1]: from http.cookies import SimpleCookie 

In [2]: cookie = SimpleCookie('x=1; json={"myVal":1}; y=2') 

In [3]: cookie.keys() 
Out[3]: dict_keys(['x']) 

In [4]: cookie = SimpleCookie('x=1; y=2') 

In [5]: cookie.keys() 
Out[5]: dict_keys(['y', 'x']) 

JSON文字列が欠落しているだけでなく、それ以降の値も表示されます。私は今、これがPython関連のバグかどうか、文字はすべてクッキーで使用されるべきであると思っていますか?

答えて

2

現在、オブジェクトを値として使用しようとしていますが、そのオブジェクトのJSON文字列表現ではありません。あなたは次のように、あなたの引数文字列の中にJSON文字列をエスケープする必要があります。

>>> from http.cookies import SimpleCookie 
>>> cookie = SimpleCookie(r'x=1; json="{\"myVal\":1}"; y=2') 
         #^note raw string ^and single backslashes 
>>> cookie.keys() 
dict_keys(['json', 'x', 'y']) 

不正な形式の文字列が静かに限り、それは行くことができるように消費され、残りはいえ、不時着していることを奇妙に思えるん。私はあなたの入力のためにValueErrorか何かを期待します。 The parserは正規表現と一致するものがなくなると結果を止めて返します。

3.3.2上で動作するように見えたが、3.5.2で動作しません(と、それは どこでもを動作するはずのように私には、見えない Marius's answer考える

、;暗黙のJSONパッケージのインポート?!)私はそれが変わったときに探しに行きました。安定しているようだ。そこから

3.3.0 (default, Jul 7 2016, 10:47:41) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['x', 'y', 'json']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['x', 'y', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['x', 'y', 'json']) <Morsel: json='{'> 

3.3.1 (default, Jul 7 2016, 10:53:06) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['json', 'x', 'y']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['json', 'x', 'y']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['json', 'x', 'y']) <Morsel: json='{'> 

3.3.2 (default, Jul 6 2016, 22:02:23) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['json', 'y', 'x']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['json', 'y', 'x']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['json', 'y', 'x']) <Morsel: json='{'> 

# ...loses 'json' 

3.3.3 (default, Jul 7 2016, 10:57:02) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['x', 'y']) None 
Jonathan dict_keys(['x', 'y', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['x', 'y']) None 

3.3.4 (default, Jul 7 2016, 10:59:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['y', 'x']) None 
Jonathan dict_keys(['y', 'x', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['y', 'x']) None 

3.3.5 (default, Jul 7 2016, 11:01:45) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['y', 'x']) None 
Jonathan dict_keys(['json', 'y', 'x']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['y', 'x']) None 

# ...and now 'y'! 

3.3.6 (default, Jul 7 2016, 11:03:40) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['x']) None 
Jonathan dict_keys(['json', 'x', 'y']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['x']) None 

:Mac OS Xが3.3のために、以下の結果が得られる上

from http.cookies import SimpleCookie 
import json 
from sys import version 

print(version) 

cookie1 = SimpleCookie('x=1; json=json.dumps({"myVal":1}); y=2') 
print('Marius ', cookie1.keys(), repr(cookie1.get('json')), sep='\t') 

cookie2 = SimpleCookie(r'x=1; json="{\"myVal\":1}"; y=2') 
print('Jonathan', cookie2.keys(), repr(cookie2.get('json')), sep='\t') 

cookie3 = SimpleCookie('x=1; json={"myVal":1}; y=2') 
print('Bernhard', cookie3.keys(), repr(cookie3.get('json')), sep='\t') 

pyenv:このスクリプトを使用して

3.4.4 (default, Jul 7 2016, 11:13:43) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['x']) None 
Jonathan dict_keys(['json', 'y', 'x']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['x']) None 

3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] 
Marius  dict_keys(['x']) None 
Jonathan dict_keys(['x', 'json', 'y']) <Morsel: json="{\"myVal\":1}"> 
Bernhard dict_keys(['x']) None 

バージョンが3.3から.6以降は明らかにセキュリティ上の修正であった。 this bug reportを参照してください。また、これはそう、3.2.6に適用した:

3.2.4 (default, Jul 7 2016, 11:05:33) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['y', 'x', 'json']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['y', 'x', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['y', 'x', 'json']) <Morsel: json='{'> 

3.2.5 (default, Jul 7 2016, 11:07:15) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['y', 'x', 'json']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['y', 'x', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['y', 'x', 'json']) <Morsel: json='{'> 

# ...loses 'y'?! 

3.2.6 (default, Jul 7 2016, 11:09:00) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] 
Marius  dict_keys(['x', 'json']) <Morsel: json='json.dumps({'> 
Jonathan dict_keys(['y', 'x', 'json']) <Morsel: json='{"myVal":1}'> 
Bernhard dict_keys(['x', 'json']) <Morsel: json='{'> 

この動作は異なる同じバグ修正と3.3.6にあります!実際には、クッキー内のjsonの値を'{'に設定し、残りの値は破棄し、yに設定します。

私はという結論:

  1. 私のバージョンは、より広く適用可能です。
  2. マリウスのバージョンは、元のバージョンと同じキーを持っているため、OPを実際には助けません。
  3. 3.2と3.3でこの機能で何か変わったことが起こっていました!
0

これは、Python 3.3.2およびそれ以前のバージョンでキーとして'json'が含まれています

>>> import json 
>>> from http.cookies import SimpleCookie 
>>> cookie = SimpleCookie('x=1; json=json.dumps({"myVal":1}); y=2') 
>>> cookie.keys() 
dict_keys(['y', 'x', 'json']) 

ただし、期待どおり値はないかもしれません。

>>> print(cookie) 
Set-Cookie: json=json.dumps({ 
Set-Cookie: x=1 
Set-Cookie: y=2 
+0

あなたが正しいです:私が持っている必要がありますインポートjson行が追加されました。私はこれをテストし、出力は正しいです。 –

+0

私はPython 3.3.2バージョン –

+0

http://labs.codecademyを使用しています。Pythonのいくつかのバージョンでこのコードが動作することを証明する –