Skip to main content

Replace host and domain names in text under various encoding schemes.

Project description

Host Replace

A Python package for replacing host and domain names in text under common encoding schemes.

Features

  • Replace hostnames in text under common encodings (URL, HTML entity) while avoiding partial matches
  • Replacements are encoded in the same way
  • Supports UTF-8 string and byte inputs
  • Supports unqualified hostnames and IPv4 addresses
  • Partial support for case preservation

See sample.txt for detailed examples.

Installation

pip install host-replace

Usage

CLI

usage: hostreplace [-h] [-o OUTPUT] -m MAPPING [-v] [input]

Replace hostnames and domains based on a provided mapping.

positional arguments:
  input                 input file to read from. If not provided, read from stdin

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        output file to write the replaced content. If not provided, write to stdout
  -m MAPPING, --mapping MAPPING
                        JSON file that contains the host mapping dictionary (e.g., {"web.example.com": "www.example.net"})
  -v, --verbose         display the replacements made

API

host_map: dict of str:str mappings input_text: str or bytes

replacer = HostnameReplacer(host_map)
output_text = replacer.apply_replacements(input_text)

Limitations

  • Does not detect encoded uppercase characters. This typically occurs only when an entire hostname (not just the special characters) is URL or entity encoded.

  • Preserving the case of individual characters is not supported. For example, if we were mapping "WWW.example.com" to "example.org", would we capitalize anything?

  • Similar ambiguity applies to post-encoding casing (e.g., "%2F" vs "%2f"; "&#x2f" vs "&#X2f"), which can lead to inconsistent representation.

  • Does not process binary data beyond searching for exact byte sequences. Encodings that are not straight character-to-sequence translations (such as base64) are not supported.

  • Hostnames beginning with a hex code are ambiguous when preceded by "%". For example, should "%00example.com" match "example.com" or "00example.com"?

  • International domains have not been tested.

  • IPv6 is not supported.

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

host_replace-0.1.6.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

host_replace-0.1.6-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file host_replace-0.1.6.tar.gz.

File metadata

  • Download URL: host_replace-0.1.6.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for host_replace-0.1.6.tar.gz
Algorithm Hash digest
SHA256 d13ed8ac0e1e683bbfc41fa80c7319d9c3d6ba45205c67e4a6695fedadf8afda
MD5 9fdb155eefd21e2c6224f2feffff4041
BLAKE2b-256 19f57a5d40da60f4852d2a4bc86d3c8e7b2a12ff3651c87f6157dd2193348ec6

See more details on using hashes here.

File details

Details for the file host_replace-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for host_replace-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a35f46e1f5f3c327ffa548df680b85070bf53affa3418414fddfaf7cf52e75aa
MD5 d3dae26002ac011b3f93645d5089db3a
BLAKE2b-256 52ae4e5ed06e895a039fda838bffc5dc023b58c9f32a6f992b0042e768ea83e3

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