gpt4 book ai didi

python-2.7 - python : "TypeError: ' Response' object has no attribute '__getitem__'

转载 作者:行者123 更新时间:2023-12-01 01:00:40 24 4
gpt4 key购买 nike

我试图在创建帐户和发布之前研究这个问题。我的研究没有定论,主要是因为我是 Python 新手。

问题:我正在尝试匹配 if 语句以确保在运行我的主代码之前服务器已启动并正在运行。

不起作用的解决方案集:
1) 我尝试了以下 if 语句
2)我尝试通过管道传递变量以查找前导/结束空格。
3) 我试过 response.split
4) 我试过 响应[:3] 匹配前 3 个字符并得到上述错误

脚本将做什么:如果 elasticsearch 服务器已启动并正在运行,它会返回“< Response [200] >”。

代码:

Import requests    
es_server = "IP:PORT"
response = requests.get("http://" + es_server)
if response == '<Response [200]>':
print "yep"

如果我尝试 response.text(),我会收到 unicode 错误

最佳答案

requests.get的结果不是字符串而是 Response持有各种信息的对象。 <Response [200]>只是该对象的字符串表示,但不等于对象本身。见 here对于某些文档。

如果你想测试响应的状态码,你应该像这样检查:

if response.status_code == 200:
print "yep"

关于python-2.7 - python : "TypeError: ' Response' object has no attribute '__getitem__' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29264455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com