简体   繁体   中英

Check if a string is encoded/quoted in python

I'm using a Python handler to respond to an HTTP API. I'm reading some url params into the handler. There are some params which needs mandatory encoded strings. How can I check if the value of certain parameter is a regular string or a encoded string?

handler?u=https%3A%2F%2Fstackoverflow.com Pass.

vs

handler?u=https://stackoverflow.com Should fail as this is a plain string.

Appreciate your help.

You can compare the string with its unquoted version:

>>> import urlparse
>>> s1 = 'handler?u=https%3A%2F%2Fstackoverflow.com'
>>> s2 = 'handler?u=https://stackoverflow.com'
>>> urlparse.unquote(s1) == s1
False
>>> urlparse.unquote(s2) == s2
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM