Skip to main content

A super-fast user agent string parser

Project description

fastuaparser

A super-fast user-agent string parser

Usage

from fastuaparser import parse_ua

# Parse the header info
client_os = parse_ua(headers.get("user-agent", ""))

# Split in browser-type and OS
client, os = client_os.partition(' - ')

The parse_ua() function never raises an exception, just tries to make as much from whatever you feed it.

A bit less precise, a whole lot faster

This code is less precise than https://github.com/ua-parser/uap-python, in that it marks rare browsers as either "Browser" or as the browser that it's based upon (e.g. IceWeasel becomes Firefox). It also marks all bots as simply "Bot" and does not care about TV's that have a browser. Other than that, this function is quite accurate and passes ua-parser's test suite.

If you can live with the above restrictions (you probably can if you're using this to e.g. monitor your website's traffic) then use this function: it's over 100 times faster that ua_parser!

On the ua-parser's set of ua strings, it renders close to 1M strings per second (in my Mac M1).

Docs

def parse_ua(s: str, always_include_os: bool = False) -> str:
    """Parse the given user-agent string and return a short description.

    The returned string has the form "client - os".

    The client is "Browser" if unknown. The os is "Other" if unknown
    and may contain a string "Mobile", "Tablet" or "Desktop. When the
    ua does not represent a browser (but e.g. a bot or wget) the os is
    omitted (unless always_include_os is True).
    """
    ...

Origins

This code used to be part of my time tracking app, which went open source, and I rolled some components (like this one) into their own little project.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastuaparser-0.1.4.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastuaparser-0.1.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastuaparser-0.1.4.tar.gz.

File metadata

  • Download URL: fastuaparser-0.1.4.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for fastuaparser-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c0b9113d17e40705dbf4582ca1303c703e2ea03ffdb2f0507afe267a317a3e79
MD5 e7cbda374ca8e2d33d8f704e650f86ab
BLAKE2b-256 38c85f8d0f39e5827555598859248fa84f8633493266507ddb902590e6488edf

See more details on using hashes here.

File details

Details for the file fastuaparser-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: fastuaparser-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for fastuaparser-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9f84be99b435c73849d408b6cd161bdc752881054a7731a810d0deb8fd89842e
MD5 eaed1a36204c7903ea741af9880249bc
BLAKE2b-256 15e039fa18142fd1e9de6e207409dd8e5f90c1cc1346a838ce81a250c3a195fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page