Jupyter:XSRF cookie与POST不匹配

我正在尝试使用Jupyter rest API将使用运行在本地Anaconda上的python程序传输到Docker容器内的本地Jupter。

我pipe理已经成功执行一个requests.get()成功之后 – 通过一点点如何input令牌。

现在我想现在执行requests.post()命令来传送文件。

组态:

  1. 在Docker工具箱上运行的本地Docker容器

    • docker版本17.04.0-ce,build立4845c56
    • 张量stream量/张量stream量 Jupyter最新版本安装
    • jupyter_kernel_gateway == 0.3.1
  2. 在Windows 10机器上运行的本地Anaconda v。4.3.14

码:

token = token_code_provided_by_jupyter_at_startup api_url = "http://192.168.99.100:8888/api/contents" # getting the file's data from disk and converting into a json file cwd = os.getcwd() file_location = cwd+r'\Resources\Test\test_post.py' payload = open(file_location, 'r').read() b64payload = base64.encodestring(payload) body = json.dumps({ 'content':b64payload, 'name': 'test_post.py', 'path': '/api/contents/', 'format': 'base64', 'type':'file' }) # getting the xsrf cookie client = requests.session() client.get('http://192.168.99.100:8888/') csrftoken = client.cookies['_xsrf'] headers ={'Content-type': 'application/json', 'X-CSRFToken':csrftoken, 'Referer':'http://192.168.99.100:8888/api/contents', 'token':token} response = requests.post(api_url, data=body, headers=headers, verify=True) 

返回错误

[W 12:22:36.710 NotebookApp] 403 POST / api / contents(192.168.99.1):XSRF cookie与POST参数不匹配[W 12:22:36.713 NotebookApp] 403 POST / api / contents(192.168.99.1)4.17ms referer = http://192.168.99.100:8888/api/contents

实际上,使用标头令牌进行authentication时,不需要xsrf cookie。

 headers = {'Authorization': 'token ' + token} 

参考Jupyter笔记本文档。

http://jupyter-notebook.readthedocs.io/en/latest/security.html