我正在尝试使用Jupyter rest API将使用本地Anaconda上运行的python程序传输到docker容器中的本地Jupyter。
我已经成功地在如何输入令牌之后成功地执行了request.get()。
现在我想执行一个requests.post()命令来传输文件。
配置:
在用于Windows的docker工具箱上运行的本地docker容器
在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
答案 0 :(得分:0)
实际上,在使用标头令牌进行身份验证时,不需要xsrf cookie。
headers = {'Authorization': 'token ' + token}
参考Jupyter笔记本文档。
http://jupyter-notebook.readthedocs.io/en/latest/security.html