Read Bittorrent Code Diary 3

xiaoxiao2021-03-06  76

#! / usr / bin / env python

# Written by bram cohen and myers carpenter # see license.txt for license information # # file name: btdownloadgui.py # reading diary: 2004-9-2 # author: zfive5 (马 不 不 之 [蓑, So-fishing cold river Snow] # # Today, I decided that the system opens the seed file to analyze the BT client. This is a good understanding, I analyze the source code is done in the Window2000 environment, double click under the resource manager .torrent # File, will then see the BT client that we are familiar with, let us choose the file directory. . . . # 知 知, 文件 文件 文件 调 入 入 入 入 入 入 注 入 入 打 打 打 打 打 打 如 打 打 打 打 如 打 打 如 如 打 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如 如"#" Content Type "=" Application / X-Bittorrent "# [HKEY_CLASS_ROOT / BITTORENT] # @ =" Torrent File "#" EditFlags "= HEX: 00, 00, 01, 100 # # [HKEY_CLASS_ROOT / BITTORRENT / shell ] # @ = "open" # [HKEY_CLASS_ROOT / BITTORRENT / shell / edit] # @ = "Edit Torrent with Torrentedit" # [HKEY_CLASES_ROOT / BITTORRENT / Shell / Edit / Command] n = int (n) h, r = Divmod (n, 60 * 60) m, sec = divmod (r, 60) IF H> 1000000: Return '' IF H> 0: Return '% D Hour% 02D min% 02D sec'% (H, M, SEC ELSE: RETURN '% D min% 02D sec'% (M, SEC)

WXEVT_INVOKE = wxneWeventType ()

# 东东, 东东, I am not clear, you can skip Def evt_invoke (win, func): win.connect (-1, -1, wxevt_invoke, func)

# Ditto class InvokeEvent (wxPyEvent): def __init __ (self, func, args, kwargs): wxPyEvent .__ init __ (self) self.SetEventType (wxEVT_INVOKE) self.func = func self.args = args self.kwargs = kwargs

# Download the main form class, this, I also passed the consciousness, actually the correct Class DownloadInfoframe:

# This is the constructor of the class DEF __INIT __ (Self, Flag): frame = wxframe (none, -1, 'bittorrent' version 'Download', size = wxsize (400, 250)) self.frame = frame self. Flag = flag self.uiflag = Event () self.fin = false self.last_update_time = 0 self.showing_error = falsepanel = wxpanel (frame, -1) Colsizer = wxflexgridsizer (cols = 1, vgap = 3)

Fnsizer = wxboxsizer (wxhorizontal)

self.fileNameText = wxStaticText (panel, -1, '', style = wxALIGN_LEFT) fnsizer.Add (self.fileNameText, 1, wxALIGN_BOTTOM) self.aboutText = wxStaticText (panel, -1, 'about', style = wxALIGN_RIGHT) self .aboutText.SetForegroundColour ( 'Blue') self.aboutText.SetFont (wxFont (14, wxNORMAL, wxNORMAL, wxNORMAL, True)) fnsizer.Add (self.aboutText, 0, wxEXPAND) colSizer.Add (fnsizer, 0, wxEXPAND)

Self.gauge = wxgauge (Panel, -1, Range = 1000, Style = wxga_smooth) Colsizer.add (Self.gauge, 0, WXExpand)

Gridsizer = wxflexgridsizer (COLS = 2, VGAP = 3, HGAP = 8) Gridsizer.add (WxStaticText (Panel, -1, 'Estimated Time Left:')) Self.TimeestText = WxStaticText (panel, -1, ') Gridsizer .Add (Self.TimeestText, 0, WXExpand)

gridSizer.Add (wxStaticText (panel, -1, 'Download to:')) self.fileDestText = wxStaticText (panel, -1, '') gridSizer.Add (self.fileDestText, 0, wxEXPAND) gridSizer.AddGrowableCol (1)

Rategridsizer = wxflexgridsizer (COLS = 4, VGAP = 3, HGAP = 8)

rategridSizer.Add (wxStaticText (panel, -1, 'Download rate:')) self.downRateText = wxStaticText (panel, -1, '') rategridSizer.Add (self.downRateText, 0, wxEXPAND) rategridSizer.Add (wxStaticText ( Panel, -1, 'Downloaded:')) Self.Downtotaltext = wxStaticText (Panel, -1, ') rategridsizer.add (self.downtotaltext, 0, wxexpand) Rategridsizer.add (WxStaticText (Panel, -1,' Upload Rate: ')) Self.upratetext = wxStaticText (Panel, -1,') rategridsizer.add (self.uprattext, 0, wxexpand) Rategridsizer.add (wxStaticText (Panel, -1, 'uploaded:') Self. UptotalText = wxStaticText (Panel, -1, ') rategridsizer.add (self.uptotaltext, 0, wxexpand) Rategridsizer.addgrowablecol (1) Rategridsizer.addgrowablecol (3)

Colsizer.add (Gridsizer, 0, WxExpand) Colsizer.add (Rategridsizer, 0, WxExpand) Colsizer.Add (50, 50, 0, wxexpand) Self.cancelButton = wxbutton (Panel, -1, 'Cancel') colsizer.add (Self.cancelButton, 0, wxalign_center) Colsizer.addgrowablecol (0) Colsizer.addgrowableRow (3)

border = wxBoxSizer (wxHORIZONTAL) border.Add (colSizer, 1, wxEXPAND | wxALL, 4) panel.SetSizer (border) panel.SetAutoLayout (True) EVT_LEFT_DOWN (self.aboutText, self.donate) EVT_CLOSE (frame, self.done) EVT_Button (frame, self.cancelbutton.getID (), self.done) evt_invoke (frame, self.onInvoke) self.frame.show ()

# 上 上 上 上 上 上 上 上 上 上 上 上 上

Def donate2 (Self): Open_new ('http://bitconjurer.org/bittorrent/donate.html') Def OnInvoke (Self, Event): if not self.uiflag.isset (): Apply (event.func, event. Args, Event.kwargs)

Def Invokelater (Self, Func, Args = [], kwargs = {}: if not self.uiflag.isset (): wxpostevent (Self.Frame, InvokeEvent (Func, Args, Kwargs))

Def UpdateStatus (Self, D): IF (self.last_update_time 0.1

Def onupdatestatus (Self, D): try: if D.has_key ('spewe'): Print_Spew (D ['spewe') Activity = D.Get ('Activity') FractONE = D.Get ('FractONE') Timeest = D.GET ('Timeest') Downrate = D.Get ('Downrate') upRate = D.GET ('UpRate') Downtotal = D.Get ('Downtotal') uptotal = D.Get ('Uptotal') IF Activity is not none and not self.fin: self.timeesttext.setlabel (activity) if fractiondone is not none and not self.fin: self.gauge.setValue (int (FRActionDONE * 1000)) Self.frame.SetTitle ('% D %%% s - bittorrent% s'% (int (FractionDone * 100), self.FileName, Version) IF Timest Is Not None: Self.TimeestText.setLabel (Hours) if Downrate Is Not None: Self .downRateText.setLabel ('%. 0f kib / s'% (float) / (1 << 10))) f uprate is not none: self.upratetext.setlabel ('%. 0f kib / s'%) Float / (1 << 10))))) If Downtotal IS Not None: Self.DowntotalText.setLabel ('%. 1F m'%) IF Uptotal is not none: self.uptotaltext.setLabel ('%. 1f m'% (uptotal) Self.last_Update_time = Time () Except: Print_ex () Def Finished (self): self.fin = true self.invokelater (Self.onfinishevent)

Def failed (self): self.fin = true self.invokelater (self.onfailevent)

Def error (self): if not self.showing_error: self.invokelater (Self.onerRorEvent, [ErrorMsg])

def onFinishEvent (self): self.timeEstText.SetLabel ( '! Download Succeeded') self.cancelButton.SetLabel ( 'Close') self.gauge.SetValue (1000) self.frame.SetTitle ( '% s - Upload - BitTorrent% S '% (Self.FileName, Version) Self.Downratetext.setLabel (' ') Def onfailevent (Self): Self.TimestText.setLabel (' Failed! ') Self.cancelButton.SetLabel (' close ') Self.gauge .SetValue (0) Self.downratetext.setLabel ('')

def onErrorEvent (self, errormsg): self.showing_error = True dlg = wxMessageDialog (self.frame, message = errormsg, caption = 'Download Error', style = wxOK | wxICON_ERROR) dlg.Fit () dlg.Center () dlg. ShowModal () Self.showing_ERROR = FALSE

Def Choosefile (Self, Default, Size, Saveas, Dir): if Saveas: Return Saveas f = Event () Bucket = [None] self.Invokelater (Self. Onchoosefile, [Default, Bucket, F, Size, Dir]) .wait () Return Bucket [0] Def onchoosefile (Self, Default, Bucket, F, Size, Dir): IF Dir: DL = WxDirdialog (Self.Frame, 'Choose A Directory to save to, Pick a Partial Download To Resume ', join (getcwd (), default), style = wxDD_DEFAULT_STYLE | wxDD_NEW_DIR_BUTTON) else: dl = wxFileDialog (self.frame,' Choose file to save as, pick a partial download to resume ',' ', default,' *. * ', wxsave) if DL.ShowModal ()! = wxid_ok: Self.done (none) else: bucket [0] = DL.GetPath () Self.FileNameText.setLabel ('% s (% .1f mb) '% (Default, Float (Size) / (1 << 20))) Self.TimestText.setLabel ('start up ...') Self.FileDestText.setLabel (DL.GetPath () ) Self.FileName = default self.frame.settitle (default "- bittorrent ' version) f.set () Def newpath (self): self.filedesttext.setLabel (PATH)

Def Done (self): self.uiflag.set () self.flag.set () self.frame.destroy ()

# This is the APP class of BT, BT start position, params is command line parameter class btwxapp (wxapp): Def __init __ (self, x, params): self.params = params wxapp .__ init__ (Self, x)

# This member function is a bit like the BOOL ctestv1app :: initInstance () member function (this is the result of my multi-year VC use) # This is the main window body class created and launched a thread class # Def Oninit. self): doneflag = Event () d = DownloadInfoFrame (doneflag) self.SetTopWindow (d.frame) thread = Thread (target = next, args = [self.params, d, doneflag]) thread.setDaemon (False) thread. START () RETURN 1 # This is the entry function of the BT client DEF RUN (params): try: app = btwxapp (0, params) app.mainloop () Except: print_exc ()

# 线 处理 处理, this is also the core process of BT DEXT (params, d, doneflag): TRY: # This is a bit fascia P = Join (Split (Argv [0]) [0], 'Donated') if not exists (p) and long (Time ())% 3 == 0: Open_New ('http://bitconjurer.org/bittorrent/donate.html') DLG = WxMessageDialog (D.Frame, 'BitTorrent Is Donation Supported Software.' 'Please Go to the Donation Page (Which Should Be Appearing for You NOW) And make a donation from there and donate later./n/nhave you make a donation yet?', 'Donate ! ', wxYES_NO | wxICON_INFORMATION | wxNO_DEFAULT) if dlg.ShowModal () == wxID_YES: dlg.Destroy () dlg = wxMessageDialog (d.frame,'! Thanks for your donation You will no longer be shown donation requests./n/ N ' "if you have't actual material name a Donation and is feeling guilty (as you shop!) You can always get to" "the Donation Page by Clicking T E 'About' Link In The Upper-Right Corner of The Main BitTorrent Window and " 'Donating from There.', 'Thanks!', wxok DLG.ShowModal () DLG.DESTROY () TRY: OPEN (P, ' WB '). Close () Except Ioerror, E: DLG = WxMessageDialog (D.Frame, "Sorry, But I Couldnown't set the flag to not ask you for donations in the fulure -" Str (e),' Sorry ! '

, wxok | wxicon_error) DLG.ShowModal () DLG.DESTROY () Else: DLG.DESTROY () # This is also another function we want to say (Params, D.choosefile, D.Updatestatus, D.Finished, D , DONEFLAG, 100, D.Newpath) IF not D.FIN: D.Failed () Except: Print_exc ()

# Is to call the Run function, the command line parameters he passed in: - Responsefile E: /Help.chm.torrent if __name__ == '__main__': Run (Argv [1:])

# First analysis here, (to be continued)

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

New Post(0)