我用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:
这样的代码仍然会产生错误。你知道吗
此处引发错误:
其中
platform
应该是支持包含测试的对象。None
不是这样的对象,所以显式测试它:相关问题 更多 >
编程相关推荐