区分这三个术语有点令人困惑。
如果能举例说明就更容易理解了。
Url 编码和 Url 转义是一回事..
URL 编码是将用户输入转换为 CGI 形式的过程,因此适合在网络上传输;基本上,删除 url 中存在的空格和特殊字符,用转义字符替换它们。
URL 重写改变了通常将 URL 与资源关联的方式。通常,test.com/aboutus 让我们认为它将带我们进入“关于我们”页面。但在内部,服务器可能会将用户 1 带到 /aboutus/page1.html,将用户 2 带到 /aboutus/page2.html 或任何其他资源。向最终用户公开的 URL 将为 test.com/aboutus,但呈现的资源可能不同。请注意,Url 重写是由服务器执行的。
老问题,但值得更好的答案。
术语“编码/解码”和“转义/unescape”可能被某些人互换使用,PHP模糊了区别,但它们在Python和(我相信)Perl中有些不同,其中编码(urllib.parse.urlencode)和解码(urllib.parse.parse_qsl)仅引用或主要引用查询字符串中的键值对,而转义(urllib.parse.quote 和urllib.parse.quote_plus) 是指将任何“不安全”字符替换为其 %xx 等效字符,例如
' '
变成 '%20'
:
>>> from urllib.parse import *
>>> quote('http://this.isa.test?hey=yo&this=that')
'http%3A//this.isa.test%3Fhey%3Dyo%26this%3Dthat'
>>> unquote(_)
'http://this.isa.test?hey=yo&this=that'
>>> 'http://this.isa.test?hey=yo&this=that'.split('?')[-1]
'hey=yo&this=that'
>>> parse_qsl(_)
[('hey', 'yo'), ('this', 'that')]
>>> urlencode(_)
'hey=yo&this=that'
urlencode 调用一个 quote 函数来转义任何必要的字符,默认情况下 quote_plus,它将
' '
转换为 '+'
而不是 '%20'
。继续之前的 Python 会话:
>>> 'http://this.isa.test?hey=yo mama&this=#that?'.split('?', 1)[-1]
'hey=yo mama&this=#that?'
>>> parse_qsl(_)
[('hey', 'yo mama'), ('this', '#that?')]
>>> urlencode(_)
'hey=yo+mama&this=%23that%3F'