本文整理汇总了Python中user_agents.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_pyua
def parse_pyua(self):
try:
self.df_p['pyua_browser'] = map(lambda x : parse(x).browser.family, self.ua_in)
self.df_p['pyua_device'] = map(lambda x : parse(x).device.family, self.ua_in)
self.df_p['pyua_pc'] = map(lambda x : parse(x).is_pc, self.ua_in)
self.df_p['pyua_mob'] = map(lambda x : parse(x).is_mobile, self.ua_in)
self.df_p['pyua_tab'] = map(lambda x : parse(x).is_tablet, self.ua_in)
except Exception as err:
print "pyua can't parse this string", self.ua_in
print err
return self.df_p
示例2: get_user_agent
def get_user_agent(request):
# Tries to get UserAgent objects from cache before constructing a UserAgent
# from scratch because parsing regexes.yaml/json (ua-parser) is slow
ua_string = request.META.get('HTTP_USER_AGENT', '')
if cache:
key = get_cache_key(ua_string)
user_agent = cache.get(key)
if user_agent is None:
user_agent = parse(ua_string)
cache.set(key, user_agent)
else:
user_agent = parse(ua_string)
return user_agent
示例3: emailOpen
def emailOpen(e):
d = {}
if request.cookies.get('LATrackingID'):
a = modules.getModel(models.App, appid = request.cookies.get('LATrackingID'))
d['app_id'] = a.id
d['private_ip'] = request.environ.get('REMOTE_ADDR')
d['public_ip'] = request.environ.get('HTTP_X_FORWARDED_FOR')
d['full_url'] = request.environ.get('HTTP_REFERER', '').strip().lower()
email = db.session.query(models.Email).filter_by(emailid=e).first()
if email:
d['email_id'] = email.id
else:
return jsonify(**{'status':'failure', 'description':'no such email found'})
if d['public_ip']:
g = geocoder.ip(d['public_ip'])
d['lat'], d['lng'] = g.latlng
d['city'] = g.city
d['country'] = g.country
d['state'] = g.state
d['user_agent'] = request.environ.get('HTTP_USER_AGENT')
if d['user_agent']:
user_agent = parse(d['user_agent'])
d['browser'] = user_agent.browser.family
d['is_bot'], d['is_mobile'], d['is_tablet'], d['is_pc'] = user_agent.is_bot, user_agent.is_mobile, user_agent.is_tablet, user_agent.is_pc
p = models.Visit(**d)
p.date = datetime.now()
db.session.add(p)
db.session.commit()
return jsonify(success=True, description='successfully tracked email')
示例4: getBrowser
def getBrowser(userAgent):
# check for empty or null uri
if userAgent:
user_agent = parse(userAgent)
return user_agent.browser.family.lower()
else:
return None
示例5: update_browser_num
def update_browser_num(list_br, packet):
if packet is None:
return [0,0,0,0,0]
else:
if(len(list_br) == 0):
list_br = [0,0,0,0,0]
if(not(is_http_get(packet))):
return list_br
else:
raw_ua = packet[get_http_layer(packet)].user_agent
parse_ua = parse(raw_ua)
ua_browser = parse_ua.browser.family
if "Firefox" in ua_browser:
list_br[0] = 1
else:
if "Chrome" in ua_browser:
list_br[1] = 1
else:
if "Safari" in ua_browser:
list_br[2] = 1
else:
if "Internet Exlorer" in ua_browser:
list_br[3] = 1
else:
list_br[4] = 1
return list_br
示例6: addUserAgentInfo
def addUserAgentInfo(db):
uaCompleted = set()
for ua in list(db.execute("""select useragent from access
except
select useragent from uainfo""")):
userAgent = ua[0]
if userAgent in uaCompleted:
continue
uaRec = user_agents.parse(userAgent)
if not uaRec:
continue
if uaRec.is_pc:
browserType = 'Browser'
elif uaRec.is_mobile:
browserType = 'Mobile Browser'
elif uaRec.is_bot:
browserType = 'Robot'
else:
browserType = 'unknown'
db.execute("""insert or replace into uainfo(useragent,
browser_type, ua_name, os_name, os_family) values(?, ?, ?, ?, ?)""",
(userAgent, browserType, uaRec.browser.family,
'%s %s' % (uaRec.os.family, uaRec.os.version_string), uaRec.os.family))
uaCompleted.add(userAgent)
db.commit() # commit per record in case we exit
return
示例7: connectionMade
def connectionMade(self):
log.debug("HTTP connection made.")
try:
user_agent = parse(self.headers['user-agent'])
self.clientInfo["clientos"] = user_agent.os.family
self.clientInfo["browser"] = user_agent.browser.family
try:
self.clientInfo["browserv"] = user_agent.browser.version[0]
except IndexError:
self.clientInfo["browserv"] = "Other"
except KeyError:
self.clientInfo["clientos"] = "Other"
self.clientInfo["browser"] = "Other"
self.clientInfo["browserv"] = "Other"
self.clientInfo["clientip"] = self.client.getClientIP()
self.plugins.hook()
self.sendRequest()
self.sendHeaders()
if (self.command == 'POST'):
self.sendPostData()
示例8: format_record
def format_record(r, recursive):
path = r.req["path"]
if "twostream" in path:
try:
path = r.req["referrer"].replace("https://www.govtrack.us", "")
except:
pass
if "?" in path: path = path[:path.index("?")] # ensure no qsargs
if r.req.get("query"): path += "?" + urllib.parse.urlencode({ k.encode("utf8"): v.encode("utf8") for k,v in list(r.req["query"].items()) })
if r.req['agent']:
ua = str(user_agents.parse(r.req['agent']))
if ua == "Other / Other / Other": ua = "bot"
ua = re.sub(r"(\d+)(\.[\d\.]+)", r"\1", ua) # remove minor version numbers
else:
ua = "unknown"
ret = {
"reqid": r.id,
"when": r.when.strftime("%b %-d, %Y %-I:%M:%S %p"),
"netblock": get_netblock_label(r.req['ip']) if r.req['ip'] else None,
"path": path,
"query": r.req.get('query', {}),
"ua": ua,
}
if recursive:
ret["netblock"] = ", ".join(sorted(set( get_netblock_label(rr.req["ip"]) for rr in Sousveillance.objects.filter(subject=r.subject) if rr.req["ip"] )))
ret["recent"] = [format_record(rr, False) for rr in Sousveillance.objects.filter(subject=r.subject, id__lt=r.id).order_by('-when')[0:15]]
return ret
示例9: check_cookie_present
def check_cookie_present(request):
#TODO we need to check the number of redirects in case we end up in a loop for some reason
if HTTPS_IFRAME_COOKIESETTER_URL_TO_CHECK in request.path \
and not ((settings.MEDIA_URL and request.path.startswith(settings.MEDIA_URL)) or request.path.startswith(settings.STATIC_URL)) \
and (not HTTPS_IFRAME_COOKIESETTER_ONLY_HTTPS or(HTTPS_IFRAME_COOKIESETTER_ONLY_HTTPS and request.is_secure())) \
and call_additional_checks():
#get the url to the cookiesetter view
cookiesetter_view_path = urlpath()
user_agent = parse(request.META.get('HTTP_USER_AGENT', ''))
if user_agent.browser.family in HTTPS_IFRAME_COOKIESETTER_BROWSERS \
and cookiesetter_view_path not in request.path:#these are after the initial check as it is an expensive lookup
current_absolute_url = urllib2.quote(request.build_absolute_uri().encode("utf8"))
cookies_present = True
for cookie_string in HTTPS_IFRAME_COOKIESETTER_COOKIES:
try:
cookie_token = request.COOKIES[cookie_string]
except KeyError:
cookies_present = False
if not cookies_present:
#ehck url scheme to http
redirect_url = '%s?absurl=%s' %(cookiesetter_view_path, current_absolute_url)
redirect_url = request.build_absolute_uri(redirect_url)
parsed = urlparse(redirect_url)
redirect_url = '%s://%s%s?%s' % ('http',parsed.netloc, parsed.path, parsed.query)
return False,redirect_url
requested_url = request.build_absolute_uri()
return True, requested_url
示例10: index
def index():
ua = request.headers.get('User-Agent')
user_agent = parse(ua)
user = g.user.nickname
image = g.user.image
user_id = g.user.id
return render_template('index.html',title='home',user=user,image=image,id=user_id)
示例11: Analytics
def Analytics(REQ):
from datetime import datetime
user_agent = parse(REQ.META.get('HTTP_USER_AGENT'))
Code = REQ.GET.get("sn","")
inurl = REQ.get_full_path()
os = user_agent.os.family
browser = user_agent.browser.family
ip = REQ.META.get('HTTP_X_FORWARDED_FORMETA') and REQ.META.get('HTTP_X_FORWARDED_FORMETA') or REQ.META.get('REMOTE_ADDR')
indata = {'ip': ip, 'browser': browser, 'os': os}
try:
ebusiness = ebusiness_members.objects.get(code=Code)
isIn = ebusiness.flow_analytics_set.filter(**indata).order_by('-intime')
ebusiness_flow = ebusiness.flow_analytics_set
except ObjectDoesNotExist:
isIn = flow_analytics.objects.filter(**indata).filter(ebusiness=None).order_by('-intime')
ebusiness_flow = flow_analytics.objects
if isIn.count() < 1:
indata.update({"inurl":inurl,"endurl":inurl})
CreateIn = ebusiness_flow.create(**indata)
CreateIn.save()
else:
oldTime = isIn.values()[0]['intime'].strftime('%Y%m%d')
newTime = datetime.now().strftime('%Y%m%d')
if newTime == oldTime:
oldID = isIn.values()[0]['id']
UpdataIn = ebusiness_flow.get(id=oldID)
UpdataIn.num = UpdataIn.num + 1
UpdataIn.endurl = inurl
UpdataIn.save()
else:
indata.update({"inurl":inurl,"endurl":inurl})
CreateIn = ebusiness_flow.create(**indata)
CreateIn.save()
return ''
示例12: redirect
def redirect(request, shorturl):
#send a 500 error shortURL doesn't exist
try:
urlObject = ShortUrl.objects.get(shortid=shorturl)
except:
return HttpResponseServerError()
#check UA of request
#user_agents is awesome! (pip install pyyaml ua-parser user-agents)
userAgent = parse(request.META.get('HTTP_USER_AGENT', ''))
if urlObject:
if userAgent.is_mobile:
urlObject.mobileRedirectCount += 1
urlObject.save()
return HttpResponseRedirect(urlObject.fullMobileUrl)
elif userAgent.is_tablet:
urlObject.tabletRedirectCount += 1
urlObject.save()
return HttpResponseRedirect(urlObject.fullTabletUrl)
elif userAgent.is_pc:
urlObject.desktopRedirectCount += 1
urlObject.save()
return HttpResponseRedirect(urlObject.fullDesktopUrl)
示例13: verify_browser
def verify_browser(request, min_versions=None):
""" accepts a request and tags it with browser support info
if `min_versions` is passed in, it skould be a dictionary of
'browser': version
"""
if not min_versions:
min_versions = settings.MIN_BROWSER_VERSIONS
request.user_agent = parse(request.META.get('HTTP_USER_AGENT', ''))
request.browser_unknown = True
request.browser_unsupported = False
for family, version in min_versions.items():
if request.user_agent.browser.family == family:
request.browser_unknown = False
bversion = request.user_agent.browser.version
if isinstance(bversion, (list, tuple)):
if len(bversion) > 0:
bversion = bversion[0]
else:
bversion = 0
else:
bversion = bversion
if bversion < version:
request.browser_unsupported = True
break
示例14: publish
def publish(request, dispatcher):
"initialise req from the Twisted reauest"
# First we need to transform the request into our own format
# our format is {key:value, .., cookies:{key:value}, request:request}
req = Req()
# retain multiple value args as a list
req.update(dict([(i[0], len(i[1]) > 1 and i[1] or i[1][0])
for i in request.__dict__['args'].items()]))
req.cookies = request.__dict__['received_cookies'] or {}
# if we have the relevant modules then add user agent information
if user_agents:
ua_string = request.getHeader('user-agent')
req.user_agent = user_agents.parse(ua_string or '')
else:
req.user_agent = None
req.request = request
# set up Session cache
session = request.getSession()
req.cache = ISessionCache(session)
# get the domain and port
req._v_domain = req.get_host().split(":")[0] # excludes port
# Now process the request
path = request.__dict__['path']
try:
result = dispatcher.request(path, req)
except:
raise
sys.stderr.write(DATE().time())
sys.stderr.write(path+'\n')
result="request error..."
return result
示例15: index
def index(groupKey=None):
if groupKey=='favicon.ico': abort(404)
#세션리셋
session.clear()
#ip = socket.gethostbyname(socket.gethostname())
ip = request.remote_addr
#https://pypi.python.org/pypi/user-agents/ #pip install user-agents
from user_agents import parse
user_agent = parse(request.user_agent.string)
if user_agent.is_mobile==True : platform = "mobile"
else : platform = "web"
accessLog = AccessLog(ip, platform)
db_session.add(accessLog)
try:
db_session.commit()
except exc.IntegrityError as e:
db_session.rollback()
debug = request.args.get('debug', '')
email = request.args.get('email', '')
resp = make_response( render_template('index.html', groupKey=groupKey) )
if email!='':
resp.set_cookie('email', email)
if debug!='':
resp.set_cookie('debug', debug)
if groupKey!=None:
resp.set_cookie('groupKey', groupKey)
return resp