Skip to content

Instantly share code, notes, and snippets.

@beastycoding
Forked from Paradoxis/agressive-url-encode.md
Created February 10, 2024 23:00
Show Gist options
  • Save beastycoding/4fef36ce804611821eaa849cc6e95108 to your computer and use it in GitHub Desktop.
Save beastycoding/4fef36ce804611821eaa849cc6e95108 to your computer and use it in GitHub Desktop.
Agressive URL encode

Agressive URL encode

Python based CLI tool to agressively url-encode strings, rather than just encoding non-url characters this tool will encode every character in the URL.

Usage:

Firstly make a function in your .bash_profile to call the script

function url-encode()
{
    python ~/<path to script>/url_encode.py $@
}

Now simply call the script from your command line :)

$ url-encode "foo"
foo
$ url-encode --agressive "foo"
%66%6f%6f
$ echo "foo" | url-encode
foo
$ echo "foo" | url-encode --agressive
%66%6f%6f
#!/usr/bin/env python3
from argparse import ArgumentParser
from sys import stdin, stdout, argv
from urllib.parse import quote as url_encode
def aggressive_url_encode(string):
return "".join("%{0:0>2}".format(format(ord(char), "x")) for char in string)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-a", "--agressive", help="Forcibly encode every character rather than non-url safe characters", action="store_true")
parser.add_argument("string", nargs="*")
args = parser.parse_args()
if args.agressive:
if args.string:
stdout.write(aggressive_url_encode(" ".join(args.string)))
else:
stdout.write(aggressive_url_encode(stdin.read()[:-1]))
else:
if args.string:
stdout.write(url_encode(" ".join(args.string)))
else:
stdout.write(url_encode(stdin.read()[:-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment