遇到很難搞的 url 參數

最近在弄物流方面的東西, 必須和第三方網站介接
轉址到該網站後, 該網站會把一些資訊透過 url 參數的方式帶回來給我
問題是, 他帶的資訊格式是經過 big5 編碼
而 GAE 內建的 request.get() 拿到的參數似乎預設就會用 utf-8 來解碼
也就是說我透過 request.get() 的方式拿到的參數是錯的

我後來找到的解法是這樣:

q_string = self.request.query_string
        q_string = q_string.split('&')
        paras = {}

        for i in q_string:
            temp = i.split('=')
            paras[temp[0]] = urllib.unquote(temp[1]).decode('big5')


我直接去parse url 的原始字串
然後把每一個參數拿出來先做 url unquote(即 url decode)
再把它 big5 解碼

參考: http://www.justaple.com/stapleViewer.html?id=1602a022c5da11e3bffd40404112cf76

補充(2021/03/30):
unquote 函式的作用

s = "url=%2F&email=imtesting%40tempmail.com&password=hereispassword"
print urllib.unquote(s)

 >>> url=/&email=imtesting@tempmail.com&password=hereispassword

即 urlencode 逆向,把%2F,%40这种特殊字符显示出来

留言

熱門文章