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: host-replace [-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"; "/" vs "/"), 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file host_replace-0.1.7.tar.gz
.
File metadata
- Download URL: host_replace-0.1.7.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2882949f31bf38871106d7979c5b220b464d4b315cc9dc8a69a29e5162194c42 |
|
MD5 | 82a3d879e812ecacbb317009df8b5181 |
|
BLAKE2b-256 | 9267cfd6c13437cbec3ba396aa43064abf5113b96ec574e30294ebd77381800a |
File details
Details for the file host_replace-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: host_replace-0.1.7-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d826ecc95935e4d2a4d945eb0c88e26be7e37f45c4aea698bf2bf4cbfc2dfb2 |
|
MD5 | 5ad02ed27d0520e40ff88878a511c800 |
|
BLAKE2b-256 | af24e07bf3cc400028cd3f6c63b583d75414ab738318f217820bf418ddc222d3 |