如何实现Linux的URL编码和URL解码?

Bash

1 前言

笔者由于需要通过bash shell访问一台需要通过账号密码验证的http服务下载文件,所以需要进行URL编码,故整理此文。

2 实践部分

2.1 解决依赖关系

yum install -y python

2.2 实现URL编码转换

2.2.1 部署Pythonb编码转换

echo "alias urldecode='python -c \"import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])\"'" > /etc/profile.d/urlcode.sh
echo "alias urlencode='python -c \"import sys, urllib as ul; print ul.quote_plus(sys.argv[1])\"'" >> /etc/profile.d/urlcode.sh
source /etc/profile.d/urlcode.sh

查看生成的命令别名:

cat /etc/profile.d/urlcode.sh

代码显示如下:

alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'

2.2.2 测试URL编码

urlencode '%^&'

显示如下:

%25%5E%26

2.2.3 测试URL解码

urldecode '%25%5E%26'

显示如下:

%^&

参阅文档:
https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding

没有评论

发表回复

Bash
如何Telnet自动登录路由器执行命令?

1 前言 一个问题,一篇文章,一出故事。 最近在笔者需要一个脚本自动从路由读取一些信息,因此整理本章 …

Bash
如何防止Base Shell脚本重复执行?

1 前言 一个问题,一篇文章,一出故事。 笔者最近发现脚本因为重复执行而损耗服务器性能,因此解决此问 …

Bash
如何实现Base Shell的数值百分比计算?

1 前言 一个问题,一篇文章,一出故事。 最近笔者需要通过Base Shell实现一个计算Quota …