Files
peace-maker fb35a123b4 Fix some global type hint errors (#2736)
* Fix some global type hint errors

Errors in global scope should be fixed before we're enabling strict mode in mypy and tackle the local problems.

* Remove pwnlib.util.pairwise since it's in itertools now

pairwise was added to itertools itself in 3.10.
https://docs.python.org/3/library/itertools.html#itertools.pairwise
2026-05-28 17:16:27 +02:00

60 lines
1.3 KiB
Python

"""
Database of >22,000 user agent strings
"""
__all__ = ['getall', 'random']
import os
import random as randommod
_cache: set[str] | None = None
def _load() -> set[str]:
global _cache
if _cache is None:
_cache = set()
with open(os.path.join(os.path.dirname(__file__),
'data/useragents/useragents.txt'
), 'r') as fd:
for line in fd:
if line:
_cache.add(line.strip())
return _cache
def getall() -> set[str]:
"""getall() -> str set
Get all the user agents that we know about.
Arguments:
None
Returns:
A set of user agent strings.
Examples:
>>> 'libcurl-agent/1.0' in getall()
True
>>> 'wget' in getall()
True
"""
return _load().copy()
def random() -> str:
"""random() -> str
Get a random user agent string.
Arguments:
None
Returns:
A random user agent string selected from :func:`getall`.
>>> import random as randommod
>>> randommod.seed(1)
>>> random() # doctest: +SKIP
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FunWebProducts; FunWebProducts-MyTotalSearch; iebar)'
"""
return randommod.choice(list(_load()))