python

尝试压缩包密码的小脚本

昨天翻硬盘,找到一个好东西,可惜自己加了密码自己不记得了。试了几个常用的没试出来,于是写了这么个小脚本来替我尝试。输入自己常用密码中的单词后,脚本来将这些密码组合尝试解压压缩包。呵呵,还真给解出来了。
python脚本内容如下,跑跑自己加密的压缩包还不错。

# -*- coding: utf-8 -*-
 
import sys,os
 
def IsElementUniq(list):
    """
          判断list中的元素是否为唯一的
    """
    for word in list:
        if list.count(word)>1:
            return False
 
    return True
 
def GenPswList():
    """
          要求用户输入词,并根据单词组合密码,只尝试四个单词来组合,并限制密码长度为20。写的比较挫
    """
    psw=raw_input('input a word>')
    wordlist = []
    while psw:
        wordlist.append(psw)
        psw=raw_input('input a word>')
    print wordlist
 
    global g_pswlist
    g_pswlist = []
    for word in wordlist:
        g_pswlist.append(word)
 
    for word1 in wordlist:
        for word2 in wordlist:
            locallist = [word1, word2]
            if IsElementUniq(locallist):
                tmp = word1 + word2
                if len(tmp) < 20:
                    g_pswlist.append(tmp)
 
    for word1 in wordlist:
        for word2 in wordlist:
            for word3 in wordlist:
                locallist = [word1, word2, word3]
                if IsElementUniq(locallist):
                    tmp = word1 + word2 + word3
                    if len(tmp) < 20:
                        g_pswlist.append(tmp)
 
    for word1 in wordlist:
        for word2 in wordlist:
            for word3 in wordlist:
                for word4 in wordlist:
                    locallist = [word1, word2, word3, word4]
                    if IsElementUniq(locallist):
                        tmp = word1 + word2 + word3 + word4
                        if len(tmp) < 20:
                            g_pswlist.append(tmp)
 
    print 'gen psw is:', g_pswlist
 
def TestUnZipPack(filename):
    """
          尝试用密码来解压压缩包
    """
 
    command = ""
    for psw in g_pswlist:
        command = "7z e -p%s -y %s" %(psw,filename)
        print command
        ret = os.system(command)
        if ret == 0:
            print 'right psw is ', psw
            break
 
def main(filename):
    GenPswList()
    TestUnZipPack(filename)
 
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'argv error'
        print 'example:test_7z_psw.py 1.7z'
        sys.exit(1)
 
    main(sys.argv[1])

顺手还把这个小脚本打了个exe的包,没有python环境的朋友可以用这个:test_7z_psw

星期四, 十二月 3rd, 2009 python 没有评论

VC6编写python扩展

        有些C/C++的代码要在Python中要用到,又懒得转成python,所以就写成python的扩展来调用,^_^。

        以下是我尝试后,在VC6下编写python扩展的过程。

        系统环境:VC6 + Python-2.5.4

        1、下载Python-2.5.4源码。

        2、解压,打开D:\Python-2.5.4\PC\VC6\pcbuild.dsw,编译,D:\Python-2.5.4\PC\VC6\下得到python25.dll、python25_d.dll、python25.lib、python25_d.lib。

        3、使用VC6建立一个动态链接库工程,拷贝D:\Python-2.5.4\PC\example_nt\example.c到工程目录下,并添加到工程中。

        4、设置工程。
            打开tools->options->directories,添加D:\PYTHON-2.5.4\INCLUDE 到 includes files中,添加D:\PYTHON-2.5.4\PC\VC6 到 Library files中。
            打开Progect->Settings,将Win32 Debug->Link->Output file name修改为example_d.pyd,将Win32 Release->Link->Output file name修改为example.pyd

        5、编译。

        6、尝试调用:

D:\MY Project\testpymodule\Release>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.foo()
Hello, world
>>>

        7、OK,打完收工。 附件为测试的工程:py_module_example

Tags: , ,

星期五, 八月 21st, 2009 python 没有评论

python学习笔记(1)

学习笔记,放硬盘里可能哪天就丢了,还是放这吧。

1,异常

dive into python,6.1有这么一个例子:

>>> fsock = open("/notthere", "r")
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
IOError: [Errno 2] No such file or directory: '/notthere'
>>> try:
...     fsock = open("/notthere")
... except IOError:
...     print "The file does not exist, exiting gracefully"
... print "This line will always print"
The file does not exist, exiting gracefully
This line will always print

那么当异常不在except IOError里面呢?

try:
    fsock = open("/notthere")
except  ImportError:
    print "The file does not exist, exiting gracefully"
print "This line will always print"
C:\>test.py
Traceback (most recent call last):
  File "C:\1.py", line 2, in <module>
    fsock = open("/notthere")
IOError: [Errno 2] No such file or directory: '/notthere'

可以看到当异常匹配不上except时,还会由python处理。

所以,当对异常类型不了解或者懒得了解的话还是直接except后不要加类型的好:

try:
    fsock = open("/notthere")
except:
    print "The file does not exist, exiting gracefully"
print "This line will always print"
C:\>test.py
The file does not exist, exiting gracefully
This line will always print

2,

  try:
      import termios, TERMIOS
  except ImportError:
      try:
          import msvcrt
      except ImportError:
          try:
              from EasyDialogs import AskPassword
          except ImportError:
              getpass = default_getpass
          else:
              getpass = AskPassword
      else:
          getpass = win_getpass
  else:
      getpass = unix_getpass

else是当没有异常的时候执行,也就是正常的时候执行
finally是无论如何都会执行,下面的例子中是用了两个try和finally是为了确保关闭句柄。(如果放第一个try块外,虽然也会关闭句柄,但是open的时候也可能出错导致并没有句柄)

        try: 
            fsock = open(filename, "rb", 0)
            try:                           
                fsock.seek(-128, 2)
                tagdata = fsock.read(128)
            finally:
                fsock.close()              
            .
            .
            .
        except IOError:
            pass

3,文件打开模式,好奇怪,难道不能以rwb方式打开?:

>>> os.listdir(".")
['bin', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'Microsoft.VC90.
CRT.manifest', 'msvcr90.dll', 'NEWS.txt', 'pycairo-wininst.log', 'pygobject-wini
nst.log', 'pygtk-wininst.log', 'python.exe', 'python26.dll', 'pythonw.exe', 'REA
DME.txt', 'Removepycairo.exe', 'Removepygobject.exe', 'Removepygtk.exe', 'Script
s', 'share', 'tcl', 'Tools', 'w9xpopen.exe']
>>> test = open("NEWS.txt", "rwb")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 22] invalid mode ('rwb') or filename: 'NEWS.txt'
>>> test = open("NEWS.txt", "rb")
>>> test.close()
>>> test = open("NEWS.txt", "wb")
>>>

Tags: ,

星期一, 八月 3rd, 2009 python 没有评论