faker.providers.internet

Subpackages

Package Contents

Classes

BaseProvider

_IPv4Constants

IPv4 network constants used to group networks into different categories.

Provider

Functions

unidecode(→ str)

lowercase(→ Callable)

slugify(→ Callable)

slugify_unicode(→ Callable)

choices_distribution(→ Sequence[T])

Attributes

ElementsType

localized

faker.providers.internet.unidecode(txt: str) str
faker.providers.internet.lowercase(fn: Callable) Callable
faker.providers.internet.slugify(fn: Callable) Callable
faker.providers.internet.slugify_unicode(fn: Callable) Callable
faker.providers.internet.choices_distribution(a: Sequence[T], p: Optional[Sequence[float]], random: Optional[random.Random] = None, length: int = 1) Sequence[T]
class faker.providers.internet.BaseProvider(generator: Any)
__provider__ = 'base'
__lang__: Optional[str]
__use_weighting__ = False
language_locale_codes
locale() str

Generate a random underscored i18n locale code (e.g. en_US).

language_code() str

Generate a random i18n language code (e.g. en).

random_int(min: int = 0, max: int = 9999, step: int = 1) int

Generate a random integer between two integers min and max inclusive while observing the provided step value.

This method is functionally equivalent to randomly sampling an integer from the sequence range(min, max + 1, step).

Sample

min=0, max=15

Sample

min=0, max=15, step=3

random_digit() int

Generate a random digit (0 to 9).

random_digit_not_null() int

Generate a random non-zero digit (1 to 9).

random_digit_above_two() int

Generate a random digit above value two (2 to 9).

random_digit_or_empty() Union[int, str]

Generate a random digit (0 to 9) or an empty string.

This method will return an empty string 50% of the time, and each digit has a 1/20 chance of being generated.

random_digit_not_null_or_empty() Union[int, str]

Generate a random non-zero digit (1 to 9) or an empty string.

This method will return an empty string 50% of the time, and each digit has a 1/18 chance of being generated.

random_number(digits: Optional[int] = None, fix_len: bool = False) int

Generate a random integer according to the following rules:

  • If digits is None (default), its value will be set to a random integer from 1 to 9.

  • If fix_len is False (default), all integers that do not exceed the number of digits can be generated.

  • If fix_len is True, only integers with the exact number of digits can be generated.

Sample

fix_len=False

Sample

fix_len=True

Sample

digits=3

Sample

digits=3, fix_len=False

Sample

digits=3, fix_len=True

random_letter() str

Generate a random ASCII letter (a-z and A-Z).

random_letters(length: int = 16) Sequence[str]

Generate a list of random ASCII letters (a-z and A-Z) of the specified length.

Sample

length=10

random_lowercase_letter() str

Generate a random lowercase ASCII letter (a-z).

random_uppercase_letter() str

Generate a random uppercase ASCII letter (A-Z).

random_elements(elements: ElementsType[T] = ('a', 'b', 'c'), length: Optional[int] = None, unique: bool = False, use_weighting: Optional[bool] = None) Sequence[T]

Generate a list of randomly sampled objects from elements.

Set unique to False for random sampling with replacement, and set unique to True for random sampling without replacement.

If length is set to None or is omitted, length will be set to a random integer from 1 to the size of elements.

The value of length cannot be greater than the number of objects in elements if unique is set to True.

The value of elements can be any sequence type (list, tuple, set, string, etc) or an OrderedDict type. If it is the latter, the keys will be used as the objects for sampling, and the values will be used as weighted probabilities if unique is set to False. For example:

# Random sampling with replacement
fake.random_elements(
    elements=OrderedDict([
        ("variable_1", 0.5),        # Generates "variable_1" 50% of the time
        ("variable_2", 0.2),        # Generates "variable_2" 20% of the time
        ("variable_3", 0.2),        # Generates "variable_3" 20% of the time
        ("variable_4": 0.1),        # Generates "variable_4" 10% of the time
    ]), unique=False
)

# Random sampling without replacement (defaults to uniform distribution)
fake.random_elements(
    elements=OrderedDict([
        ("variable_1", 0.5),
        ("variable_2", 0.2),
        ("variable_3", 0.2),
        ("variable_4": 0.1),
    ]), unique=True
)
Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’), unique=False

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’), unique=True

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’), length=10, unique=False

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’), length=4, unique=True

Sample
elements=OrderedDict([

(“a”, 0.45), (“b”, 0.35),

(“c”, 0.15), (“d”, 0.05),

]), length=20, unique=False

Sample
elements=OrderedDict([

(“a”, 0.45), (“b”, 0.35), (“c”, 0.15), (“d”, 0.05),

]), unique=True

random_choices(elements: ElementsType[T] = ('a', 'b', 'c'), length: Optional[int] = None) Sequence[T]

Generate a list of objects randomly sampled from elements with replacement.

For information on the elements and length arguments, please refer to random_elements() which is used under the hood with the unique argument explicitly set to False.

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’)

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’), length=10

Sample
elements=OrderedDict([

(“a”, 0.45), (“b”, 0.35), (“c”, 0.15), (“d”, 0.05),

])

Sample
elements=OrderedDict([

(“a”, 0.45), (“b”, 0.35), (“c”, 0.15), (“d”, 0.05),

]), length=20

random_element(elements: ElementsType[T] = ('a', 'b', 'c')) T

Generate a randomly sampled object from elements.

For information on the elements argument, please refer to random_elements() which is used under the hood with the unique argument set to False and the length argument set to 1.

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’)

Sample size=10
elements=OrderedDict([

(“a”, 0.45), (“b”, 0.35), (“c”, 0.15), (“d”, 0.05),

])

random_sample(elements: ElementsType[T] = ('a', 'b', 'c'), length: Optional[int] = None) Sequence[T]

Generate a list of objects randomly sampled from elements without replacement.

For information on the elements and length arguments, please refer to random_elements() which is used under the hood with the unique argument explicitly set to True.

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’)

Sample

elements=(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’), length=3

randomize_nb_elements(number: int = 10, le: bool = False, ge: bool = False, min: Optional[int] = None, max: Optional[int] = None) int

Generate a random integer near number according to the following rules:

  • If le is False (default), allow generation up to 140% of number. If True, upper bound generation is capped at 100%.

  • If ge is False (default), allow generation down to 60% of number. If True, lower bound generation is capped at 100%.

  • If a numerical value for min is provided, generated values less than min will be clamped at min.

  • If a numerical value for max is provided, generated values greater than max will be clamped at max.

  • If both le and ge are True, the value of number will automatically be returned, regardless of the values supplied for min and max.

Sample

number=100

Sample

number=100, ge=True

Sample

number=100, ge=True, min=120

Sample

number=100, le=True

Sample

number=100, le=True, max=80

Sample

number=79, le=True, ge=True, min=80

numerify(text: str = '###') str

Generate a string with each placeholder in text replaced according to the following rules:

  • Number signs (‘#’) are replaced with a random digit (0 to 9).

  • Percent signs (‘%’) are replaced with a random non-zero digit (1 to 9).

  • Dollar signs (‘$’) are replaced with a random digit above two (2 to 9).

  • Exclamation marks (‘!’) are replaced with a random digit or an empty string.

  • At symbols (‘@’) are replaced with a random non-zero digit or an empty string.

Under the hood, this method uses random_digit(), random_digit_not_null(), random_digit_or_empty(), and random_digit_not_null_or_empty() to generate the random values.

Sample

text=’Intel Core i%-%%##K vs AMD Ryzen % %%##X’

Sample

text=’!!! !!@ !@! !@@ @!! @!@ @@! @@@’

lexify(text: str = '????', letters: str = string.ascii_letters) str

Generate a string with each question mark (‘?’) in text replaced with a random character from letters.

By default, letters contains all ASCII letters, uppercase and lowercase.

Sample

text=’Random Identifier: ??????????’

Sample

text=’Random Identifier: ??????????’, letters=’ABCDE’

bothify(text: str = '## ??', letters: str = string.ascii_letters) str

Generate a string with each placeholder in text replaced according to the following rules:

  • Number signs (‘#’) are replaced with a random digit (0 to 9).

  • Question marks (‘?’) are replaced with a random character from letters.

By default, letters contains all ASCII letters, uppercase and lowercase.

Under the hood, this method uses numerify() and and lexify() to generate random values for number signs and question marks respectively.

Sample

letters=’ABCDE’

Sample

text=’Product Number: ????-########’

Sample

text=’Product Number: ????-########’, letters=’ABCDE’

hexify(text: str = '^^^^', upper: bool = False) str

Generate a string with each circumflex (‘^’) in text replaced with a random hexadecimal character.

By default, upper is set to False. If set to True, output will be formatted using uppercase hexadecimal characters.

Sample

text=’MAC Address: ^^:^^:^^:^^:^^:^^’

Sample

text=’MAC Address: ^^:^^:^^:^^:^^:^^’, upper=True

faker.providers.internet.ElementsType
faker.providers.internet.localized = True
class faker.providers.internet._IPv4Constants

IPv4 network constants used to group networks into different categories. Structure derived from ipaddress._IPv4Constants.

Excluded network list is updated to comply with current IANA list of private and reserved networks.

_network_classes: Dict[str, ipaddress.IPv4Network]
_private_networks: List[ipaddress.IPv4Network]
_excluded_networks: List[ipaddress.IPv4Network]
class faker.providers.internet.Provider(generator: Any)

Bases: faker.providers.BaseProvider

safe_domain_names: faker.providers.ElementsType[str] = ('example.org', 'example.com', 'example.net')
free_email_domains: faker.providers.ElementsType[str] = ('gmail.com', 'yahoo.com', 'hotmail.com')
tlds: faker.providers.ElementsType[str] = ('com', 'com', 'com', 'com', 'com', 'com', 'biz', 'info', 'net', 'org')
hostname_prefixes: faker.providers.ElementsType[str] = ('db', 'srv', 'desktop', 'laptop', 'lt', 'email', 'web')
uri_pages: faker.providers.ElementsType[str] = ('index', 'home', 'search', 'main', 'post', 'homepage', 'category', 'register', 'login', 'faq',...
uri_paths: faker.providers.ElementsType[str] = ('app', 'main', 'wp-content', 'search', 'category', 'tag', 'categories', 'tags', 'blog',...
uri_extensions: faker.providers.ElementsType[str] = ('.html', '.html', '.html', '.htm', '.htm', '.php', '.php', '.jsp', '.asp')
http_methods: faker.providers.ElementsType[str] = ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH')
user_name_formats: faker.providers.ElementsType[str] = ('{{last_name}}.{{first_name}}', '{{first_name}}.{{last_name}}', '{{first_name}}##', '?{{last_name}}')
email_formats: faker.providers.ElementsType[str] = ('{{user_name}}@{{domain_name}}', '{{user_name}}@{{free_email_domain}}')
url_formats: faker.providers.ElementsType[str] = ('www.{{domain_name}}/', '{{domain_name}}/')
image_placeholder_services: faker.providers.ElementsType[str] = ('https://picsum.photos/{width}/{height}', 'https://dummyimage.com/{width}x{height}',...
replacements: Tuple[Tuple[str, str], Ellipsis] = ()
_to_ascii(string: str) str
email(safe: bool = True, domain: Optional[str] = None) str
safe_domain_name() str
safe_email() str
free_email() str
company_email() str
free_email_domain() str
ascii_email() str
ascii_safe_email() str
ascii_free_email() str
ascii_company_email() str
user_name() str
hostname(levels: int = 1) str

Produce a hostname with specified number of subdomain levels.

>>> hostname()
db-01.nichols-phillips.com
>>> hostname(0)
laptop-56
>>> hostname(2)
web-12.williamson-hopkins.jackson.com
domain_name(levels: int = 1) str

Produce an Internet domain name with the specified number of subdomain levels.

>>> domain_name()
nichols-phillips.com
>>> domain_name(2)
williamson-hopkins.jackson.com
domain_word() str
dga(year: Optional[int] = None, month: Optional[int] = None, day: Optional[int] = None, tld: Optional[str] = None, length: Optional[int] = None) str

Generates a domain name by given date https://en.wikipedia.org/wiki/Domain_generation_algorithm

Return type

str

tld() str
http_method() str

Returns random HTTP method https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

Return type

str

url(schemes: Optional[List[str]] = None) str
Parameters

schemes – a list of strings to use as schemes, one will chosen randomly. If None, it will generate http and https urls. Passing an empty list will result in schemeless url generation like “://domain.com”.

Returns

a random url string.

_get_all_networks_and_weights(address_class: Optional[str] = None) Tuple[List[ipaddress.IPv4Network], List[int]]

Produces a 2-tuple of valid IPv4 networks and corresponding relative weights

Parameters

address_class – IPv4 address class (a, b, or c)

_get_private_networks_and_weights(address_class: Optional[str] = None) Tuple[List[ipaddress.IPv4Network], List[int]]

Produces an OrderedDict of valid private IPv4 networks and corresponding relative weights

Parameters

address_class – IPv4 address class (a, b, or c)

_get_public_networks_and_weights(address_class: Optional[str] = None) Tuple[List[ipaddress.IPv4Network], List[int]]

Produces a 2-tuple of valid public IPv4 networks and corresponding relative weights

Parameters

address_class – IPv4 address class (a, b, or c)

_random_ipv4_address_from_subnets(subnets: List[ipaddress.IPv4Network], weights: Optional[List[int]] = None, network: bool = False) str

Produces a random IPv4 address or network with a valid CIDR from within the given subnets using a distribution described by weights.

Parameters
  • subnets – List of IPv4Networks to choose from within

  • weights – List of weights corresponding to the individual IPv4Networks

  • network – Return a network address, and not an IP address

Returns

_exclude_ipv4_networks(networks: List[ipaddress.IPv4Network], networks_to_exclude: List[ipaddress.IPv4Network]) List[ipaddress.IPv4Network]

Exclude the list of networks from another list of networks and return a flat list of new networks.

Parameters
  • networks – List of IPv4 networks to exclude from

  • networks_to_exclude – List of IPv4 networks to exclude

Returns

Flat list of IPv4 networks

ipv4_network_class() str

Returns a IPv4 network class ‘a’, ‘b’ or ‘c’.

Returns

IPv4 network class

ipv4(network: bool = False, address_class: Optional[str] = None, private: Optional[str] = None) str

Returns a random IPv4 address or network with a valid CIDR.

Parameters
  • network – Network address

  • address_class – IPv4 address class (a, b, or c)

  • private – Public or private

Returns

IPv4

ipv4_private(network: bool = False, address_class: Optional[str] = None) str

Returns a private IPv4.

Parameters
  • network – Network address

  • address_class – IPv4 address class (a, b, or c)

Returns

Private IPv4

ipv4_public(network: bool = False, address_class: Optional[str] = None) str

Returns a public IPv4 excluding private blocks.

Parameters
  • network – Network address

  • address_class – IPv4 address class (a, b, or c)

Returns

Public IPv4

ipv6(network: bool = False) str

Produce a random IPv6 address or network with a valid CIDR

mac_address() str
port_number(is_system: bool = False, is_user: bool = False, is_dynamic: bool = False) int

Returns a network port number https://tools.ietf.org/html/rfc6335

Parameters
  • is_system – System or well-known ports

  • is_user – User or registered ports

  • is_dynamic – Dynamic / private / ephemeral ports

Return type

int

uri_page() str
uri_path(deep: Optional[int] = None) str
uri_extension() str
uri(schemes: Optional[List[str]] = None, deep: Optional[int] = None) str
Parameters
  • schemes – a list of strings to use as schemes, one will chosen randomly. If None, it will generate http and https uris. Passing an empty list will result in schemeless uri generation like “://domain.com/index.html”.

  • deep – an integer specifying how many path components the URI should have..

Returns

a random url string.

slug(value: Optional[str] = None) str

Django algorithm

image_url(width: Optional[int] = None, height: Optional[int] = None, placeholder_url: Optional[str] = None) str

Returns URL to placeholder image Example: http://placehold.it/640x480

Parameters
  • width – Optional image width

  • height – Optional image height

  • placeholder_url – Optional template string of image URLs from custom placeholder service. String must contain {width} and {height} placeholders, eg: https:/example.com/{width}/{height}.

Return type

str

iana_id() str

Returns IANA Registrar ID https://www.iana.org/assignments/registrar-ids/registrar-ids.xhtml

Return type

str

ripe_id() str

Returns RIPE Organization ID https://www.ripe.net/manage-ips-and-asns/db/support/organisation-object-in-the-ripe-database

Return type

str

nic_handle(suffix: str = 'FAKE') str

Returns NIC Handle ID https://www.apnic.net/manage-ip/using-whois/guide/person/

Return type

str

nic_handles(count: int = 1, suffix: str = '????') List[str]

Returns NIC Handle ID list

Return type

list[str]