BT source code learning experience (3): encoding of seed files

Source: Internet
Author: User

BTSource codeLearning Experience (3): encoding of seed files
Author: Wolfenstein

The author of Bt uses a simple and easy-to-understand encoding method to design the seed file. This encoding method can easily encode various data types in Python, such as strings, integers, lists, and dictionaries. In addition, nesting of types, such as a list of elements and a list, can be well processed.
The BitTorrent/bencode. py module is responsible for encoding and decoding. The function bencode can encode complex Python data types. Does this function mean BT encode? It completes the task by calling itself recursively.
First, he judges the data type to be encoded, then calls the corresponding encoding function encode_func [type (x)] based on this type, and defines the following types of encoding functions:
Encode_int, which is used to encode inttype and longtype. Encode it as a string with the value "I" to add the letter "E ". That is to say, the integer 19851122 will be encoded as "i19851122e" in the file.
Encode_string, which is used to encode the stringtype. Encode the string with a colon ":" followed by the string itself. For example, helloworld is encoded as "10: helloworld" and stored in a file.
Encode_list, which is responsible for encoding the listtype and tupletype types. The encoding method is a letter "L", and then recursively calls the corresponding encoding function to bencode all the elements of the list or tuples, and end with an "e.
Encode_dict, which is responsible for encoding the dicttype. The encoding method is a letter "D", and each element is recursively processed. In dicttype, each element is composed of a key and value pair. First, encode the key by adding ":" to the actual value. Because the key is usually a simple value, you can encode it like this. Then perform bencode on the value, and add "E" to the end.
By analyzing the above encoding functions, we can see that complex objects can be restored without ambiguity after being encoded in this encoding method. The BT seed file is such a complex object (Dictionary type ). After learning the encoding method, the next time you introduce the seed file, you only need to explain the situation of each element contained in this dictionary type, the process of saving as a file and reading from the file does not need to be explained.

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.