OSDN Git Service

Initial commit
authortanyuliang <tanyuliang2@gmail.com>
Thu, 9 Jul 2015 11:59:28 +0000 (19:59 +0800)
committertanyuliang <tanyuliang2@gmail.com>
Thu, 9 Jul 2015 11:59:28 +0000 (19:59 +0800)
Axel.py [new file with mode: 0644]
FriendList.py [new file with mode: 0644]
LoginGui.py [new file with mode: 0644]

diff --git a/Axel.py b/Axel.py
new file mode 100644 (file)
index 0000000..8f6f44a
--- /dev/null
+++ b/Axel.py
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# filename: Axel.py
+import sys
+import os
+import time
+import urllib
+from threading import Thread
+local_proxies = {'http': 'http://131.139.58.200:8080'}
+class AxelPython(Thread, urllib.FancyURLopener):
+    def __init__(self, threadname, url, filename, ranges=0, proxies={}):
+        Thread.__init__(self, name=threadname)
+        urllib.FancyURLopener.__init__(self, proxies)
+        self.name = threadname
+        self.url = url
+        self.filename = filename
+        self.ranges = ranges
+        self.downloaded = 0 
+    def run(self):
+        try:
+            self.downloaded = os.path.getsize( self.filename )
+        except OSError:
+            self.downloaded = 0
+        self.startpoint = self.ranges[0] + self.downloaded
+        if self.startpoint >= self.ranges[1]:
+            print 'Part %s has been downloaded over.' % self.filename
+            return    
+        self.oneTimeSize = 16384 #16kByte/time
+        print 'task %s will download from %d to %d' % (self.name, self.startpoint, self.ranges[1])
+        self.addheader("Range", "bytes=%d-%d" % (self.startpoint, self.ranges[1])) 
+        self.urlhandle = self.open( self.url )
+        data = self.urlhandle.read( self.oneTimeSize )
+        while data:
+            filehandle = open( self.filename, 'ab+' )
+            filehandle.write( data )
+            filehandle.close()
+            self.downloaded += len( data )
+            #print "%s" % (self.name)
+            #progress = u'\r...'
+            data = self.urlhandle.read( self.oneTimeSize )
+def GetUrlFileSize(url, proxies={}):
+    urlHandler = urllib.urlopen( url, proxies=proxies )
+    headers = urlHandler.info().headers
+    length = 0
+    for header in headers:
+        if header.find('Length') != -1:
+            length = header.split(':')[-1].strip()
+            length = int(length)
+    return length
+def SpliteBlocks(totalsize, blocknumber):
+    blocksize = totalsize/blocknumber
+    ranges = []
+    for i in range(0, blocknumber-1):
+        ranges.append((i*blocksize, i*blocksize +blocksize - 1))
+    ranges.append(( blocksize*(blocknumber-1), totalsize -1 ))
+    return ranges
+def islive(tasks):
+    for task in tasks:
+        if task.isAlive():
+            return True
+    return False
+def paxel(url, output, blocks=6, proxies=local_proxies):
+    size = GetUrlFileSize( url, proxies )
+    ranges = SpliteBlocks( size, blocks )
+    threadname = [ "thread_%d" % i for i in range(0, blocks) ]
+    filename = [ "tmpfile_%d" % i for i in range(0, blocks) ]
+    tasks = []
+    for i in range(0,blocks):
+        task = AxelPython( threadname[i], url, filename[i], ranges[i] )
+        task.setDaemon( True )
+        task.start()
+        tasks.append( task )
+    time.sleep( 2 )
+    while islive(tasks):
+        downloaded = sum( [task.downloaded for task in tasks] )
+        process = downloaded/float(size)*100
+        show = u'\rFilesize:%d Downloaded:%d Completed:%.2f%%' % (size, downloaded, process)
+        sys.stdout.write(show)
+        sys.stdout.flush()
+        time.sleep( 0.5 )
+    filehandle = open( output, 'wb+' )
+    for i in filename:
+        f = open( i, 'rb' )
+        filehandle.write( f.read() )
+        f.close()
+        try:
+            os.remove(i)
+            pass
+        except:
+            pass
+    filehandle.close()
\ No newline at end of file
diff --git a/FriendList.py b/FriendList.py
new file mode 100644 (file)
index 0000000..c548f01
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/python
+#coding=utf-8
+import wx
+import sys
+from xml.etree import ElementTree as ET
+class MyFrame(wx.Frame):
+    def OnClose(self, event):
+        dlg = wx.MessageDialog(self, 
+            "Do you really want to close this application?",
+            "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
+        result = dlg.ShowModal()
+        dlg.Destroy()
+        if result == wx.ID_OK:
+            sys.exit()
+    def __init__(self, parent, id, title,user):
+        wx.Frame.__init__(self, parent, id, title,
+                          wx.DefaultPosition, wx.Size(200, 450))
+        hbox = wx.BoxSizer(wx.HORIZONTAL)
+        vbox = wx.BoxSizer(wx.VERTICAL)
+        panel1 = wx.Panel(self, -1)
+        self.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (-1, -1),
+                                wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)
+        root = self.tree.AddRoot('My friend')
+        fm = self.tree.AppendItem(root, 'Family people')
+        tr = self.tree.AppendItem(root, 'Teacher')
+        cl = self.tree.AppendItem(root,'ClassMate')
+        fr = self.tree.AppendItem(root, 'Friend')
+        ot = self.tree.AppendItem(root, 'Other')
+        per=ET.parse(user)
+        p=per.findall('/family/person')
+        q=per.findall('/friend/person')
+        r=per.findall('/teacher/person')
+        for x in p:
+            print
+        for oneper in p:  #找出person节点
+                for child in oneper.getchildren(): #找出person节点的子节点
+                        self.tree.AppendItem(fm, child.text)
+        for x in r:
+            print
+        for oneper in r:  
+                for child in oneper.getchildren(): 
+                        self.tree.AppendItem(tr, child.text)
+        #cl = self.tree.AppendItem(pl, 'Dev Language')
+        #sl = self.tree.AppendItem(pl, 'Shell')
+        for x in q:
+                print
+        for oneper in q:  
+                for child in oneper.getchildren(): 
+                        self.tree.AppendItem(fr, child.text)        
+        vbox.Add(self.tree, 1, wx.EXPAND)
+        hbox.Add(panel1, 1, wx.EXPAND)
+        panel1.SetSizer(vbox)
+        self.SetSizer(hbox)
+        self.Center()
+        self.Bind(wx.EVT_CLOSE, self.OnClose)
\ No newline at end of file
diff --git a/LoginGui.py b/LoginGui.py
new file mode 100644 (file)
index 0000000..fb0c75d
--- /dev/null
@@ -0,0 +1,59 @@
+#!/usr/bin/python
+# encoding: utf-8
+import os
+import wx
+import urllib 
+import urllib2 
+import sys
+import ssl
+import FriendList
+import Axel
+from xml.etree import ElementTree
+from Crypto.Cipher import AES
+from binascii import b2a_hex, a2b_hex
+class prpcrypt():
+    def __init__(self, key):
+        self.key = key
+        self.mode = AES.MODE_CBC    
+    def decrypt(self, text):
+        cryptor = AES.new(self.key, self.mode, self.key)
+        plain_text = cryptor.decrypt(a2b_hex(text))
+        return plain_text.rstrip('\0')
+class LoginFrame(wx.Frame):
+    def __init__(self, parent, id, title, size):
+        wx.Frame.__init__(self, parent, id, title)
+        self.SetSize(size)
+        self.Center()
+        self.passWordLabel = wx.StaticText(self, label = "UserName", pos = (10, 50), size = (120, 25))
+        self.userNameLabel = wx.StaticText(self, label = "Password", pos = (40, 100), size = (120, 25))
+        self.userName = wx.TextCtrl(self, pos = (120, 47), size = (150, 25))
+        self.passWord= wx.TextCtrl(self, pos = (120, 97), size = (150, 25),style=wx.TE_PASSWORD)
+        self.loginButton = wx.Button(self, label = 'Login', pos = (80, 145), size = (130, 30))
+        self.loginButton.Bind(wx.EVT_BUTTON, self.login)
+        self.Show()
+    def login(self, event):
+            ssl._create_default_https_context = ssl._create_unverified_context
+            Axel.paxel('https://7nar2o.com5.z0.glb.clouddn.com/user/' + self.userName.GetValue() + ".xml", self.userName.GetValue() + '.xml', blocks=4, proxies={} )
+            response = os.path.exists(self.userName.GetValue()+'.xml')
+            if response == 'FALSE':
+                wx.MessageBox('Download Error', 'Error', 
+                wx.OK | wx.ICON_ERROR)
+            else:
+                root = ElementTree.fromstring(open(self.userName.GetValue() + ".xml").read())
+                node_find = root.find('login')
+                pwd_txt = ''.join( [ str(x) for x in node_find.attrib.values()])
+                passwd0 = pc.decrypt(pwd_txt)
+                if self.passWord.GetValue()==passwd0:
+                    wx.MessageBox('Login Successful', 'Information', 
+                    wx.OK | wx.ICON_INFORMATION)
+                    self.Hide()
+                    frame = FriendList.MyFrame(None, id=-1, title="Friend List",user=self.userName.GetValue() + '.xml')
+                    frame.Show(True)
+                else:
+                    wx.MessageBox('Your Password is wrong', 'Try it again', 
+                    wx.OK | wx.ICON_ERROR) 
+if __name__ == '__main__':
+    pc = prpcrypt('keyskeyskeyskeys')
+    app = wx.App()
+    LoginFrame(None, -1, title = "Login", size = (280, 200))
+    app.MainLoop()
\ No newline at end of file