【发布时间】:2010-10-11 15:48:58
【问题描述】:
如果页面被 iphone/ipod touch 访问,我需要以不同的方式呈现页面。我想信息在请求对象中,但是语法是什么?
【问题讨论】:
标签: iphone python google-app-engine web-applications
如果页面被 iphone/ipod touch 访问,我需要以不同的方式呈现页面。我想信息在请求对象中,但是语法是什么?
【问题讨论】:
标签: iphone python google-app-engine web-applications
这是我一直在寻找的语法,适用于 iphone 和 ipod touch:
uastring = self.request.headers.get('user_agent')
if "Mobile" in uastring and "Safari" in uastring:
# do iphone / ipod stuff
【讨论】:
AttributeError: 'WSGIRequest' object has no attribute 'headers'。
This article 概述了通过检查 HTTP_USER_AGENT 代理变量来检测 iPhone 的几种方法。根据您要在哪里进行检查(HTML 级别、Javascript、CSS 等),我相信您可以将其推断到您的 Python 应用程序中。对不起,我不是蟒蛇人。 8^D
【讨论】:
Apple 网站上的Using the Safari on iPhone User Agent String 文章指出了 iPhone 和 iPod touch 的不同用户代理。
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3
Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3
Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/XXXXX Safari/525.20
【讨论】:
以下是如何在 Django 中将其实现为中间件,假设您在 appengine 上使用了该中间件。
class DetectiPhone(object):
def process_request(self, request):
if 'HTTP_USER_AGENT' in request.META and request.META['HTTP_USER_AGENT'].find('(iPhone') >= 0:
request.META['iPhone'] = True
基本上在 HTTP_USER_AGENT 中查找“iPhone”。请注意,iPod Touch 的签名与 iPhone 稍有不同,因此是广泛的“iPhone”搜索,而不是更严格的搜索。
【讨论】:
如果您使用标准 webapp 框架,用户代理将位于请求实例中。这应该足够好了:
if "iPhone" in request.headers["User-Agent"]:
# do iPhone logic
【讨论】:
检查用户代理。会是
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
我不确定如何使用 appengine 执行此操作,但可以在此处找到等效的 PHP 代码:http://www.mattcutts.com/blog/iphone-user-agent/
【讨论】:
import os
class MainPage(webapp.RequestHandler):
@login_required
def get(self):
userAgent = os.environ['HTTP_USER_AGENT']
if userAgent.find('iPhone') > 0:
self.response.out.write('iPhone support is coming soon...')
else:
self.response.out.write('Hey... you are not from iPhone...')
【讨论】: