【问题标题】:python 2.7 requests.get() returning cookie raising TypeErrorpython 2.7 requests.get() 返回 cookie 引发 TypeError
【发布时间】:2014-11-07 03:28:59
【问题描述】:

我正在对我们的内部服务器进行简单的 HTTP 请求身份验证,获取 cookie 然后访问 Cassandra RESTful 服务器以获取数据。返回 cookie 时 requests.get() 阻塞。

我有一个成功提取数据的 curl 脚本,我宁愿在纯 python 中处理响应 JSON 数据。

我在下面做错了什么的任何线索?我转储了 cookie,它看起来不错,和我的 curl cookie 非常相似。

克雷格


import requests
import rtim

# this makes the auth and gets the cookie returned, save the cookie
myAuth = requests.get(rtim.rcas_auth_url, auth=(rtim.username, rtim.password),verify=False)
print myAuth.status_code
authCookie=myAuth.headers['set-cookie']

IXhost='xInternalHostName.com:9990'
mylink='http:/%s/v1/BONDISSUE?format=JSONARRAY&issue.isin=%s' % (IXhost, 'US3133XK4V44')

# chokes on next line .... doesn't like the Cookie format
r = requests.get(mylink, cookies=authCookie)
(Pdb) next
TypeError: 'string indices must be integers, not str'

【问题讨论】:

    标签: python python-2.7 cookies http-request


    【解决方案1】:

    我认为问题出在最后一行:

    r = requests.get(mylink, cookies=authCookie)

    requests 假定 cookies 参数是一个字典,但您将字符串对象 authCookie 传递给它。

    当请求尝试将字符串 authCookie 视为字典时引发异常。

    【讨论】:

    • 谢谢 Rikka,所以我使用 authCookie 作为值 我使用什么字符串作为 dict 键?
    • 我不确定。 authCookie 的价值是什么?我认为 authCookie 本身应该包含 cookie 的键和值,但是是字符串格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多