Package: uadetector
WSGI Middleware and web framework extensions for handling User-Agent. Thanks towoothee, UADetector supports various User-Agents. This library respects tok0kubun/rack-user_agent.Installation$ pip install uadetectorUsageWSGI middlewareThis middleware provides auadetector.useragent.UserAgentobject to handling User-agents.fromwsgiref.simple_serverimportmake_server# import middlewarefromuadetectorimportUADetectordefapp(environ,start_response):start_response('200 OK',[('Content-Type','text/plain')])# get 'UserAgent' object from environ dict.ua=environ.get('uadetector.useragent')ua.user_agent#=> "Mozilla/5.0 (Macintosh; ..."ua.device_type#=> "pc"ua.os#=> "Mac OSX"ua.browser#=> "Chrome"ua.from_pc#=> Trueua.from_smartphone#=> Falsereturn[ua.os.encode('utf-8')]# Apply middlewareapplication=UADetector(app)if__name__=="__main__":withmake_server('127.0.0.1',8000,application)asserver:print("Serving on port 8000...")server.serve_forever()You can also replace the key ofenvironor theUserAgentclass.fromuadetector.useragentimportUserAgentclassMyUserAgent(UserAgent):# Write your custom codes.# Apply middlewareapplication=UADetector(app,envorion_key='your.favorite.key'useragent_class='path.to.MyUserAgent')See alsoWSGI example.Web framework extensionsSome web frameworks provide a way to extend in a different way from WSGI Middleware. This library provide shortcuts according to that way.Caution: I do not actively support individual frameworks. If you are worried, you should use WSGIMiddleware.DjangoYou can use Django’sMIDDLEWARE.# settings.pyMIDDLEWARE=[# Add UADetecorMiddleware'uadetector.django.middleware.UADetectorMiddleware',# ... omit ...]# views.pydefindex_view(request):print(request.ua.from_smartphone)# => True or False# ... omit ...Customize property name of request object and replace UserAgent class.# settings.pyUADETECTOR_REQUEST_PROPERTY_NAME='agent'# => You can use "request.agent"UADETECTOR_USERAGENT_CLASS='path.to.MyUserAgent'See alsoDajngo example.Tips:Switch templates based on User-Agent(usingdjango-variantmpl).PyramidYou can useconfig.add_request_method.fromuadetector.pyramidimportua_propdefindex(request):print(request.ua.from_smartphone)# => True or False# ... omit ...withConfigurator()asconfig:config.add_route('index','/')config.add_view(index,route_name='index')config.add_request_method(ua_prop(),name='ua',reify=True)# ... omit ...Customize property name of request object and replace UserAgent class.config.add_request_method(ua_prop('path.to.MyUserAgent'),name='agent',# => You can use "request.agent"reify=True)See alsoPyramid example.FlaskYou can useFlask Extension.fromflaskimportFlask,requestfromuadetector.flaskimportUADetectorapp=Flask(__name__)UADetector(app)@app.route('/')defindex():print(request.ua.from_smartphone)# => True or False# ... omit ...Customize property name of request object and replace UserAgent class.app=Flask(__name__)app.config['UADETECTOR_USERAGENT_CLASS']='path.to.MyUserAgent'app.config['UADETECTOR_REQUEST_PROPERTY_NAME']='agent'# => You can use "request.agent"UADetector(app)See alsoFlask example.TornadoYou can use customRequestHandler.fromuadetector.tornado.webimportRequestHandlerclassIndexHandler(RequestHandler):defget(self):print(self.request.ua.from_smartphone)# => True or False# ... omit ...Customize property name of request object and replace UserAgent class.fromtornado.optionsimportdefinefromuadetector.tornado.webimportRequestHandlerdefine('uadetector_request_property_name',default='agent',# => You can use "self.request.agent")define('uadetector_useragent_class',default='path.to.MyUserAgent')classIndexHandler(RequestHandler):See alsoTornado example.UserAgentList of properties ofuadetector.useragent.UserAgentobject.attrsUserAgent.device_variantUserAgent.device_typeUserAgent.osUserAgent.os_versionUserAgent.browserUserAgent.browser_versionUserAgent.browser_vendorhelpersUserAgent.from_pcUserAgent.from_smartphoneUserAgent.from_mobilephoneUserAgent.from_applianceUserAgent.from_crawlerdetectorsUserAgent.smartphone_versionUserAgent.from_iphoneUserAgent.from_ipadUserAgent.from_ipodUserAgent.from_androidUserAgent.from_android_tabletUserAgent.from_windows_phoneUserAgent.from_iosUserAgent.from_android_osTipsIf you want aUserAgentobject simply from the User-Agent string, Please useget_useruseragent.fromuadetectorimportget_useragentua_string="Mozilla/5.0 (iPhone; CPU iPhone OS ..."ua=get_useragent(ua_string)us.from_smartphone# => True# Use custom useragent classua=get_useragent(ua_string,useragent_class='path.to.MyUserAgent')SupportSupport latest 3 minor versions.Python 3.4, 3.5, 3.6Django 1.10, 1.11, 2.0Pyramid 1.7, 1.8, 1.9Flask 0.10, 0.11, 0.12Tornado 4.5, 4.6, 4.7LicenseMIT LicenseAuthorstell-k <ffk2005 at gmail.com>History0.1.3(Feb 20, 2018)Lazy parsing User-Agent string.0.1.2(Feb 19, 2018)First release