Skip to content

Instantly share code, notes, and snippets.

@jimmydo
Forked from johnboxall/device_detection.py
Created May 31, 2011 20:26
Show Gist options
  • Save jimmydo/1001205 to your computer and use it in GitHub Desktop.
Save jimmydo/1001205 to your computer and use it in GitHub Desktop.
import re
# Some mobile browsers which look like desktop browsers.
RE_MOBILE = re.compile(r"(iphone|ipod|blackberry|android.+mobile|palm|windows\s+ce)", re.I)
RE_DESKTOP = re.compile(r"(windows|linux|os\s+[x9]|solaris|bsd)", re.I)
RE_BOT = re.compile(r"(spider|crawl|slurp|bot)")
def is_desktop(user_agent):
"""
Anything that looks like a phone isn't a desktop.
Anything that looks like a desktop probably is.
Anything that looks like a bot should default to desktop.
"""
return not bool(RE_MOBILE.search(user_agent)) and \
bool(RE_DESKTOP.search(user_agent)) or \
bool(RE_BOT.search(user_agent))
def get_user_agent(request):
# Some mobile browsers put the User-Agent in a HTTP-X header
return request.headers.get('HTTP_X_OPERAMINI_PHONE_UA') or \
request.headers.get('HTTP_X_SKYFIRE_PHONE') or \
request.headers.get('HTTP_USER_AGENT', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment