Read Bittorrent Code Diary 1

xiaoxiao2021-03-06  73

# Written by Petru Paler # see license.txt for license information "" "

Reading Diary: 2004-8-20 Editor: ZFive5 (Drunk Hummer [Lonely Boat " Before you start, you can use Make to make a .torrent file to see the content, two files in my E: / 1 / directory, DIR is:

2004-08-31 15:33

.

2004-08-31 15:33

.

2004-08-31 14:44 10 1.txt

2004-08-31 15:35 11 2.txt

The contents of the .torrent documentation is as follows (here the newline and space notes): D 8: Announce 30: http://127.0.0.1: 6969 / announce 13: Creation Date i1093944351e 4: info d 5: files l d 6: length i10e 4: path l 5: 1.txt e e d 6: length i11e 4: path l 5: 2.txt e e e 4: name 1: 1 12: piece length i32768e 6: PIECES 20: Xin ^ ^ G? 觑 'Maximum hemp (D4 44 5E E4 DE 67 D1 18 03 D3 55 27 D7 EE 8A A2 AF 71 EB D8) (This is a file block of the BT file block 20 Bit SHA Message Abstract) E 3: NFO D EE

It is like this: announce: http://127.0.0.1: 6969 / announce code date: 1093944351 (second number) info files {length = 10 (byte) Path = 1.Txt, length = 11 (word Festival) PATH = 2.txt} Name = 1 Piece Length = 32768 (Each file size) PIECES = D4 44 5E E4 DE 67 D1 18 03 D3 55 27 D7 EE 8A A2 AF 71 EB D8 NFO = NULL (empty)

Here there are several types of data structures to explain: Integer: IE, for example: I1093944351E String: N: For example: 30: http: //127.0.0.1: 6969 / announce list: L e, for example: L5: 1.TXTE Dictionary: D e, for example: D6: lengthi10e4: pathl5: 1.txteed6: lengthi11e4: Pathl5: 2.txteee

"" "#bt source code is better than all explanations Import re from CSTRINGIO IMPORT STRINGIO

INT_FILTER = Re.Compile ('(0 | -? [1-9] [0-9] *) E')

# Decoding integer function DEF decode_int (x, f): m = int_filter.match (x, f) IF m is None: raise valueerror return (long (M.Group (1)), m.end ()) String_filter = Re.Compile ('(0 | [1-9] [0-9] *):')

# 解 字 字函 (x, f): m = string_filter.match (x, f) IF m is none: raise valueerror l = int (M.Group (1)) s = m.end () return X [S: S L], S L)

# Decode list function DEF decode_list (x, f): r = [] while x [f]! = 'E': V, f = bdecode_rec (x, f) R.Append (v) Return (R, F 1 )

# Decoded Dictionary Function Def decode_dict (x, f): r = {} lastkey = None while x [f]! = 'E': k, f = decode_string (x, f) if LastKey is not none and lastkey> = K : Raise valueerror lastkey = k v, f = bdecode_rec (x, f) R [k] = v retturn (R, F 1)

# 对 内 解 _ (x, f): t = x [f] if t == 'i': return decode_int (x, f 1) Elif T == 'L': Return Decode_List (x, F 1) Elif T == 'D': Return Decode_Dict (x, f 1) else: return decode_string (x, f)

# 对 外 解 d: 对 :er =er =er =er =er 的 =er 的 的 的 的 的 的 印t

# Test decoding function DEF TEST_BDECODE (): TRY: BDECode ('0: 0: 0: PASSTY: BDECode (' IE ') Assert 0 Except ValueError: Pass try: bdecode (' i341foo382e ') Assert 0 except ValueError: pass assert bdecode ( 'i4e') == 4L assert bdecode ( 'i0e') == 0L assert bdecode ( 'i123456789e') == 123456789L assert bdecode ( 'i-10e') == -10L try: bdecode ('i-0e') Assert 0 Except Valueerror: Pass Try: BDECode ('I123') Assert 0 Except Valueerror: Pass Try: BDECode ('') Assert 0 Except ValueError: Pass Try: bdecode ('i6easd') Assert 0 Except ValueError: Pass Try: BDECode ('35208734823ljdahflajhdf') Assert 0 Except Valueerror: Pass try: bdecode ('2: Abfdjslhfld') assert 0 Except valueError: Pass assert bdecode '0:') == 'ASSERT BDECode (' 3: ABC ') ==' Abc 'assert bdecode ('10: 1234567890') == '1234567890' Try: bdecode ('02: xy ') assert 0 Except ValueError: Pass Try: BDECode ('L') Assert 0 Except valueerror: pass assert bdecode ('le') == [] Try: bdecode ('leanfdldjfh') assert 0 Except valueerror: pass assert bdecode ('l0: 0: 0: E ') == [' ',' ',' '

] Try: BDECode ('Relwjhrlewjh') Assert 0 Except valueerror: pass assert bdecode ('Li1ei2ei3ee') == [1, 2, 3] Assert BDecode ('L3: ASD2: XYE') == ['ASD', ' XY '] Assert Bdecode (' LL5: Alice3: Bobeli2Ei3eee ') == [[' Alice ',' Bob '], [2, 3]] Try: BDECode (' d ') Assert 0 Except valueerror: pass: bdecode ('Defoobar') Assert 0 Except valueerror: Pass assert bdecode ('de') == {} Assert Bdecode ('D3: agei25e4: eyees4: bluee') == {'agn': 25, 'EYES': 'Blue '} Assert bdecode (' D8: spam.mp3d6: author5: alice6: lengthi100000eee ') == {' spam.mp3 ': {' Author ':' Alice ',' Length ': 100000}} Try: BDECode (' D3 : fooe ') Assert 0 Except Valueerror: Pass try: BDECode (' Di1e0: e ') Assert 0 Except valueError: Pass try: bdecode (' D1: B0: 1: A0: E ') Assert 0 Except valueerror: Pass Try: BDECode ('D1: A0: 1: A0: E') ASSE rt 0 except ValueError: pass try: bdecode ( 'i03e') assert 0 except ValueError: pass try: bdecode ( 'l01: ae') assert 0 except ValueError: pass try: bdecode ( '9999: x') assert 0 except ValueError : Pass Try: BDECode ('l0:') Assert 0 Except valueerror: pass try: bdecode ('d0: 0:'

ASSERT 0 Except Valueerror: Pass try: bdecode ('d0:) Assert 0 Except valueerror: pass # 对 内 内 编 d d (x) = (xi) : B.Write ('I% DE'% X) Elif T IS STR: B.WRITE (Len (X), X)) Elif T in (List, Tuple): B .write ('L') for e in x: Bencode_rec (e, b) B.Write ('e') Elif T IS Diction: B.Write ('d') Keylist = x.keys () Keylist.sort ( KEYLIST: ASSERT TYPE (K) IS STR BENCODE_REC (K, B) Bencode_Rec (x [k], b) B.Write ('e') Else: Assert 0

# 对 外 编 编 (x): b = stringio () Bencode_Rec (x, b) Return B.GetValue ()

转载请注明原文地址:https://www.9cbs.com/read-121332.html

New Post(0)