Skip to main content

A random user-agent generator

Project description

ua-generator

A random user-agent generator for Python >= 3.6

Features

  • No external user-agent list. No downloads.
  • Templates are hardcoded into the code.
  • Platform and browser versions are based on real releases.
  • Client hints (Sec-CH-UA fields).

Installing

pip3 install -U ua-generator

Basic usage

import ua_generator

ua = ua_generator.generate()
print(ua) # Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/15.2 Safari/604.1.38

Customization

There are three different parameters to the generate user-agent by the certain conditions.

device = ('desktop', 'mobile')
platform = ('windows', 'macos', 'ios', 'linux', 'android')
browser = ('chrome', 'edge', 'firefox', 'safari')

All of the parameters are optional, and the types can be set more than one by using a tuple.

Customized user-agent generation:

import ua_generator

# Example 1:
ua = ua_generator.generate(device='desktop', browser=('chrome', 'edge'))
print(ua.text) # Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.145 Safari/537.36
print(ua.platform) # windows
print(ua.browser) # chrome
print(ua.ch.brands) # "Not A(Brand";v="99", "Chromium";v="108", "Google Chrome";v="108"
print(ua.ch.mobile) # ?0
print(ua.ch.platform) # "Windows"
print(ua.ch.platform_version) # "13.0.0"
print(ua.ch.bitness) # "64"
print(ua.ch.architecture) # "x86"

# Example 2:
ua = ua_generator.generate(platform=('ios', 'macos'), browser='chrome')
print(ua.text) # Mozilla/5.0 (iPhone; CPU iPhone OS 17_0_2 like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) CriOS/119.0.6045.176 Mobile/15E148 Safari/537.36
print(ua.platform) # ios
print(ua.browser) # chrome
print(ua.ch.brands) # "Not A(Brand";v="99", "Chromium";v="119", "Google Chrome";v="119"
print(ua.ch.mobile) # ?1
print(ua.ch.platform) # "iOS"
print(ua.ch.platform_version) # "17.0.2"
print(ua.ch.bitness) # "64"
print(ua.ch.architecture) # "arm"

Headers

ua = ua_generator.generate(browser=('chrome', 'edge'))

# This will return a dictionary containing the generated user-agent:
print(ua.headers.get())
{
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.43 Safari/537.36',
    'sec-ch-ua': '"Not A(Brand";v="99", "Chromium";v="103", "Google Chrome";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"'
}

# Extending the "Client Hints" by a value of the "Accept-CH" header:
print(ua.headers.accept_ch('Sec-CH-UA-Platform-Version, Sec-CH-UA-Full-Version-List'))
print(ua.headers.get())
{
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.94 Safari/537.36',
    'sec-ch-ua': '"Not A(Brand";v="99", "Chromium";v="122", "Google Chrome";v="122"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-ch-ua-platform-version': '"14.1.0"',
    'sec-ch-ua-full-version-list': '"Not A(Brand";v="99", "Chromium";v="122.0.6261.94", "Google Chrome";v="122.0.6261.94"'
}

Integrating into the requests:

import requests
import ua_generator

ua = ua_generator.generate(browser=('chrome', 'edge'))
r = requests.get('https://httpbin.org/get', headers=ua.headers.get())
print(r.text)

Integrating into the httpx:

import httpx
import ua_generator

ua = ua_generator.generate(browser=('chrome', 'edge'))
client = httpx.Client(headers=ua.headers.get())
r = client.get('https://httpbin.org/get')
print(r.text)

Integrating into the urllib:

import urllib.request
import ua_generator

ua = ua_generator.generate(browser=('chrome', 'edge'))
request = urllib.request.Request('https://httpbin.org/get', headers=ua.headers.get())
handler = urllib.request.urlopen(request)
response = handler.read().decode('utf-8')
print(response)

Issues

You can create an issue from here if you are experiencing a problem.

Author

Ekin Karadeniz (iamdual@icloud.com)

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

ua-generator-0.4.1.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

ua_generator-0.4.1-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file ua-generator-0.4.1.tar.gz.

File metadata

  • Download URL: ua-generator-0.4.1.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for ua-generator-0.4.1.tar.gz
Algorithm Hash digest
SHA256 73eac8f892ec824b6b298801dc301e0e100f2e2440b4111e890504f9b9c8c0b6
MD5 703f2bdcdb86ecc8a526726206223d4d
BLAKE2b-256 c049d66af8c2ba8742904d2ed0652f8a11e703603ebbc31ad85f6f0d259f19d6

See more details on using hashes here.

File details

Details for the file ua_generator-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: ua_generator-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for ua_generator-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ce3feea061d1baebd61bc9ab823a0b55c8ca6c107252270fedbc483b0c45be5
MD5 a5bb0079ab66bb72b1bcb8fc9ecc98f7
BLAKE2b-256 59e6a73951ef6b939a835f90be8358982f08526b21e7d620ddc6f482a99a84df

See more details on using hashes here.

Supported by

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