Python learning (28) URL encoding and decoding & signing rules requirements Practice

Source: Internet
Author: User
Tags md5 encryption

1. URL encoding and URL decoding

Open the browser, enter the "Basic Python Tutorial", click on the search, the address bar will have a long list of similar to%dfbgn this is the URL encoding corresponding search content, as follows:

Https://www.so.com/s?q=python%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B&src=srp&fr=360chrome_newtab_search &psid=9fcfb50581a9a0358a7459af7832c302

So how do you use code for URL encoding and URL decoding?

 fromUrllibImportparseURL='Http://www.baidu.com?query=python Basic Tutorials'Url_str=parse.quote_plus (URL)#URL encodingPrint(URL_STR) Baidu_url='https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=06074089_4_pg&wd=python%E5%9F%BA%E7%A1%80 %e6%95%99%e7%a8%8b&oq=python%25e5%259f%25ba%25e7%25a1%2580%25e6%2595%2599%25e7%25a8%258b&rsv_pq= bc14fc45000148d3&rsv_t=1cc4h7yncotnc5%2fejw6ufw%2fab8klfjbeowxnae8ogbhh8towwqhoy4bkrckpq6xra2jquq& Rqlang=cn&rsv_enter=0'Print(Parse.unquote_plus (Baidu_url))#URL decoding

The result of the output is:

http%3a%2f%2fwww.baidu.com%3fquery%3dpython%e5%9f%ba%e7%a1%80%e6%95%99%e7%a8%8Bhttps://www.baidu.com/ S?ie=utf-8&f=8&rsv_bp=1&tn=06074089_4_pg&wd=python Basic Tutorial &oq=python%e5%9f%ba%e7%a1%80%e6%95% 99%e7%a8%8b&rsv_pq=bc14fc45000148d3&rsv_t=1cc4h7yncotnc5/ejw6ufw/ Ab8klfjbeowxnae8ogbhh8towwqhoy4bkrckpq6xra2jquq&rqlang=cn&rsv_enter=0

After decoding can be seen to search for the basic Python tutorial

2. Requirements for signature rules

The requirements are as follows:

A, input data:
1. Request data:
vendorid=1697&poscode=pos006&ip=127.0.0.1&posversion=2.1.1.1.1&mac=;D 4-81-d7-ca-20-29; 7c-67-a2-9a-06-05;7c-67-a2-9a-06-06;7c-67-a2-9a-06-09;00-00-00-00-00-0000e0
2: Merchant Code:
1697
B, calculation rules:
1, the Merchant Code (vendorid) 1697 two times MD5 encryption to get a string astr:09b764965ce4e92bed8bd3140e22d5cf
2, the request data
vendorid=1697&poscode=pos006&ip=127.0.0.1&posversion=2.1.1.1.1&mac=;D 4-81-d7-ca-20-29; 7c-67-a2-9a-06-05;7c-67-a2-9a-06-06;7c-67-a2-9a-06-09;00-00-00-00-00-0000e0
do urldecode processing (encoded in utf-8) to get a string urldecodestr:
vendorid=1697&poscode=pos006&ip=127.0.0.1&posversion=2.1.1.1.1&mac=;D 4-81-d7-ca-20-29; 7c-67-a2-9a-06-05;7c-67-a2-9a-06-06;7c-67-a2-9a-06-09;00-00-00-00-00-0000e0
3, Urldecodestr + Astr stitching to get a string to be encrypted beforeMD5
vendorid=1697&poscode=pos006&ip=127.0.0.1&posversion=2.1.1.1.1&mac=;D 4-81-d7-ca-20-29; 7c-67-a2-9a-06-05;7c-67-a2-9a-06-06;7c-67-a2-9a-06-09;00-00-00-00-00-0000e009b764965ce4e92bed8bd3140e22d5cf
The beforeMD5 string is then encrypted to get the final signature:
6f811b63b11d92ba4a359e6dc98eee31

The steps required to decompose the above requirements are as follows:

1, request data acquisition to
2. Extract business code
3, the Merchant code two times MD5, get Astr
4, then the request data once urldecode, get url_str
5, ASTR+URL_STR for MD5

Next follow the steps for code writing

s = ' vendorid=1697&poscode=pos006&ip=127.0.0.1&posversion=2.1.1.1.1&mac=;D 4-81-d7-ca-20-29; 7c-67-a2-9a-06-05;7c-67-a2-9a-06-06;7c-67-a2-9a-06-09;00-00-00-00-00-0000e0 '

ClassSign (object):def __init__(self,req_data): Self.req_data=req_data#This writes self, other functions can also be used self.xxSelf.get_vendorid ()#It can be called in get_sign or in this call .self.get_sign ()defMD5 (SELF,S): MD=hashlib.md5 () md.update (S.encode ())returnmd.hexdigest ()defGet_vendorid (self): data_dict={} sub_list= Self.req_data.split ('&') #[vendorid=1697,poscode=pos006,ip=127.0.0.1] forIinchSub_list:k, v= I.split ('=') Data_dict[k]=v Self.vendorid= Data_dict.get ('VendorID') defget_sign (self):#Self.get_vendorid ()first=self.md5 (self.vendorid) astr=self.md5 (first) Url_str=Parse.quote_plus (self.req_data) self.sign=SELF.MD5 (astr+url_str)returnSelf.sign

Look at this code, first this class needs to pass the request data as a parameter, as a class variable. The next few functions are MD5 cryptographic functions, which have been learned before.

Get the Merchant encoding function:

When obtaining the Merchant code function, the request data is divided according to the & symbol, and the result of the segmentation is a list, which is divided into:

[vendorid=1697,poscode=pos006,ip=127.0.0.1] (list after the value of also, do not write, mainly to find the first), and then the list of elements according to = to split,

and put the result of the partition into the dictionary, this time VendorID is key,1697 is value, and then take out the VendorID value from the dictionary 1697

Signature function:

The acquisition of business code two times MD5 encryption, get ASTR, and then the requested data URL encoding, get url_str, and finally Astr+url_str MD5, is required signature

Since the constructor is executed at instantiation, we call get Merchant code and signature function inside the constructor, then after instantiation, call sign directly, can output the signature

Abc= sign(s)print(abc.sign)

Of course, you can also call the function that gets the signature in the previous way

Abc= sign(s)print(Abc.get_sign ())

The output is:

2e8d260f14fc05a2881470504fb6f9d5

Python learning (28) URL encoding and decoding & signing rules requirements Practice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.