'Google-B的Flask请求似乎是空的'

2024-09-27 21:27:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Flask写了一个网站;它返回移动和桌面的不同结果。我在服务器上使用request.user_agent.platform检查平台。你知道吗

以下是我返回android、桌面和其他移动设备的不同网站的代码:

if request.user_agent.platform == "android":
    ismobile = True
    # do something when the user is on android
for pf in ["iphone", "iPad", "ipad", "Windows Phone OS", "windows phone os", "BlackBerry", "windows phone", "Windows Phone", "WindowsPhone", "android"]:
    if pf in request.user_agent.platform:
        ismobile = True
        # do something when the user is on any other mobile device
if not ismobile:
    # do something when the user is on desktop

到目前为止,这个代码是有效的。但是当Google Bot发送请求时,会抛出一个ValueError:

TypeError
TypeError: argument of type 'NoneType' is not iterable

(通过Google标记工具测试,这里是full stack trace)。你知道吗

当Google访问我的页面时,似乎什么都不是。有人知道如何防止我的应用程序抛出错误吗?你知道吗

编辑:我忘记了:当我访问请求时抛出错误。像if request is None:这样的代码仍然会产生错误。你知道吗


Tags: the代码ifisonrequestgoogledo
1条回答
网友
1楼 · 发布于 2024-09-27 21:27:55

此处引发错误:

if pf in request.user_agent.platform:

其中platform应该是支持包含测试的对象。None不是这样的对象,所以显式测试它:

platform = request.user_agent.platform
if platform and pf in platform:

相关问题 更多 >

    热门问题