2015-09-13 78 views
1

我用Flask写了一个网站;它会为手机和桌面返回不同的结果。我在服务器上使用request.user_agent.platform来检查平台。烧瓶请求似乎没有Google-Bot

这里是我的代码为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 

到目前为止,此代码的工作。但是当谷歌博特发送一个请求,一个ValueError被抛出:

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

(与谷歌标记工具测试,这里的full stack trace)。

当Google访问我的页面时,似乎有些东西是无。有谁知道我可以如何防止我的应用程序抛出错误?

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

+0

为什么不测试'是None'呢? –

+0

@MartijnPieters我试过,但错误仍然抛出。 – palsch

+0

@Functino谢谢,编辑是完美的。 – palsch

回答

1

该错误是这里抛出:

if pf in request.user_agent.platform: 

platform其中预计将支撑容纳测试的对象。 None是不是这样一个对象,所以明确地测试它:

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

现在我知道修复无效的原因了:我使用raspberrypi“同步”我的代码。在这raspi没有空间,所以它什么都没做。错误出现是因为它没有在服务器上修复。 – palsch