这里说的urlencode非python自带的urlencode是与
初学python 风格较烂 勿喷。
# -*- coding: UTF-8 -*-
#编码
def urlencode_pl(inputs_str):
len_str=len(inputs_str)
if str=="" or len_str<=0:
return ""
index_j=0
index_i=0
result_end=""
for index_i in range(0,len_str):
index_sb=index_i+1
chs=inputs_str[index_i:index_sb]
if (chs>='A' and chs<'Z') or (chs>='a' and chs<'z') or (chs>='0' and chs<'9'):
if result_end=="":
result_end=chs
else:
result_end+=chs
elif chs==' ':
result_end+='+'
elif chs=='.' or chs=='-' or chs=='_' or chs=='*':
result_end+=chs
else:
result_end='%s%%%02X' % (result_end,ord(chs))
return result_end
#解码
def urldecode_pl(srcstr):
dststr=""
leng=len(srcstr)
temp1=""
temp2=""
i=0;
while i
temp1="\0";
temp2="\0";
if(srcstr[i:i+1]!="%" and srcstr[i:i+1]!="+"):
dststr=dststr+srcstr[i:i+1];
i=i+1;
continue
elif(srcstr[i:i]=="+"):
dststr=dststr+" ";
i=i+1;
continue
temp1=srcstr[i+1:i+2];
temp2=srcstr[i+2:i+3];
sb1=0;
sb2=0;
if(cmp(temp1,"A")>=0):
sb1=7;
if(cmp(temp1,"a")>=0):
sb2=32;
temp1=ord(temp1)-48-sb1-sb2
sb1=0;
sb2=0;
if(cmp(temp2,"A")>=0):
sb1=7;
if(cmp(temp2,"a")>=0):
sb2=32;
temp2=ord(temp2)-48-sb1-sb2
dststr=dststr+chr(temp1*16+temp2);
i=i+3;
return dststr;
#main函数
if __name__ == '__main__':
strs="sanya.ganji.com/huangye/a3/s/c/_大众"
print strs
str2=urlencode_pl(strs);
print str2;
str3=urldecode_pl(str2)
print str3;
str4=urldecode_pl(strs)
print str4
运行 python XX.py
结果如下:
原始字符串:sanya.ganji.com/huangye/a3/s/c/_大众 编码字符串:sanya.ganji.com%2Fhuangye%2Fa3%2Fs%2Fc%2F_%E5%A4%A7%E4%BC%97 编码解码字符串:sanya.ganji.com/huangye/a3/s/c/_大众