Skip to content

Instantly share code, notes, and snippets.

@graysuit
Forked from Paradoxis/agressive-url-encode.md
Created April 10, 2021 15:50
Show Gist options
  • Save graysuit/33e7e50244c74ed6aedbc55b7c435676 to your computer and use it in GitHub Desktop.
Save graysuit/33e7e50244c74ed6aedbc55b7c435676 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