工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.51099.com 注册 | 登陆
浏览模式: 标准 | 列表分类:pytyon

python3 的安装

 wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz -c

xz -d Python-3.4.3.tar.xz
tar xvf Python-3.4.3.tar
cd Python-3.4.3
./configure --prefix=/usr
make
make install
 
ln -sf /usr/bin/python3.4 /usr/bin/python

pip安装

 pip安装方法
   Pip 是安装python包的工具,提供了安装包,列出已经安装的包,升级包以及卸载包的功能。
   Pip 是对easy_install的取代,提供了和easy_install相同的查找包的功能,因此可以使用easy_install安装的包也同样可以使用pip进行安装。
   安装Pip
   Pip的安装可以通过源代码包,easy_install或者脚本。
   下面介绍一下各种安装方法:
   源代码方式:
   $ wget https://pypi.python.org/packages/source/p/pip/pip-1.2.1.tar.gz (替换为最新的包)
   $ tar xzf pip-1.2.1.tar.gz
   $ cd pip-1.2.1.tar.gz
   $ python setup.py install
   easy_install:
   $ easy_install pip
   get_pip.py 脚本:
   $ curl -0 https://raw.github.com/pypa/pip/master/contrib/get-pip.py
   $ sudo python get-pip.py
   OK, 下面来看一下Pip的使用
   安装package
   $ pip install Markdown
   列出安装的packages
   $ pip freeze
   安装特定版本的package
   通过使用==, >=, <=, >, <来指定一个版本号。
   $ pip install 'Markdown<2.0'
   $ pip install 'Markdown>2.0,<2.0.3'
   升级包
   升级包到当前最新的版本,可以使用-U 或者 --upgrade
   $ pip install -U Markdown
   卸载包
   $ pip uninstall Markdown
   查询包
   pip search "Markdown"

Python代码加密中源码保护pyc文件'字节码'的描述

Python代码加密 源码保护--pyc文件'字节码'

关于Python代码加密这一Python的“特殊功能”在网上好像是没有更多的相关介绍,但是随着越来越多的商业人士对加入Python这一阵营,所以Python代码加密这一计算机语言就显得尤为重要。
内容概要:Python代码加密,源码保护,代码保护,pyc文件

关于python代码保护在网上好像一直没有很好的介绍,可能和PYTHON的设计初衷"开源"有关,但越来越多的兄弟写的加入PYTHON阵营,开发工具用途各异,在商业应用中:Python代码加密保护就变的重要了.
下面介绍一下发布PYC文件,做简单代码保护

PYC文件是什么?

pyc文件是python的二进制编译,pyc文件比py文件拥有更快的装载速度,执行速度没有区别。因为pyc文件是二进制文件,不是明文显示,利用这一特性,发布PYC文件隐藏源代码.

命令如下:

  1. python -c "import compileall; compileall.
    compile_dir('目录')" 

 

该命令可以为一个目录下的py文件生成pyc文件(包含子目录)在windows平台上的路径书写方式为

  1. ('C:/python26/pyc') 

 

下面符一完整命令: 将目录'C:/python26/pyc'下的所有PY文件 转为PYC文件

 

  1. ?[Copy to clipboard]Download test.py1  
  2. python -c "import compileall; compileall.
    compile_dir('C:/python26/pyc')"   
     

 

如果想实现真正的加密:Python代码加密,就要改PYTHON源码,做自己的加密模块了,这方面还没深入了解,不便多说.关于代码加密这块如哪位兄弟还有更好的方法,请多指教.

python freeze.py

wget http://svn.python.org/projects/python/trunk/Tools/freeze/freeze.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/hello.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/checkextensions.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/parsesetup.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/makeconfig.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/makefreeze.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/bkfile.py
wget http://svn.python.org/projects/python/trunk/Tools/freeze/makemakefile.py

 

python freeze.py hello.py

                 from config.c:18:
/usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory
make: *** [config.o] Error 1

 

yum -y install glibc-devel

install g++-multilib

 

-----------------

(.text+0x53c1): undefined reference to `PyEval_RestoreThread'
/usr/lib/python2.4/config/libpython2.4.a(posixmodule.o): In function `posix_getcwd':
(.text+0x53d3): undefined reference to `PyString_FromString'
/usr/lib/python2.4/config/libpython2.4.a(posixmodule.o): In function `posix_getcwd':
(.text+0x53f8): undefined reference to `__stack_chk_fail_local'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

在32位机器上正常

-------------------------------

1. 把http://svn.python.org/projects/python/trunk/Tools/freeze/下面的文件下载到一个目录去,后面以其中的hello.py为例

下载:

wget http://svn.python.org/projects/python/trunk/Tools/freeze/
wget `cat index.html | grep li | awk -F\" '{print "http://svn.python.org/projects/python/trunk/Tools/freeze/"$2}'`


2. 安装python的dev包,如python2.5-dev

3. python /path/to/freeze.py /path/to/hello.py
4. make

就好了:P

或者

python /path/to/freeze.py -o dist /path/to/hello.py
然后进入dist目录,再
make

------------------------

http://wiki.python.org/moin/Freeze

深入理解Python yield

Python模块学习 ---- StringIO, cStringIO 内存文件(转)

StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO。一个简单的例子,让你对StringIO有一个感性的认识:

1   #coding=gbk
2    
3   import StringIO, cStringIO, sys
4    
5   s = StringIO.StringIO("JGood is a handsome boy")
6   s.write("JGood is a handsome boy \r\n")
7   s.write('okkkk中国')
8   s.seek(0)
9   print s.read()
10    
11   #最后4个字节
12   s.seek(-4, 2)
13   print s.read()
14    
15   #---- 结果 ----
16   #JGood is a handsome boy 
17   #okkkk中国
18   #中国

通过例子,我们看到了StringIO的行为,基本与file一致。StringIO提供了一个方法,可以方便的获取其中的数据:StringIO.getvalue()。如果使用read方法获取其中的数据,必须通过seek先设置"文件指针"的位置。

  Python标准模块中还提供了一个cStringIO模块,它的行为与StringIO基本一致,但运行效率方面比StringIO更好。但使用cStringIO模块时,有几个注意点: 1. cStringIO.StringIO不能作为基类被继承;2. 创建cStringIO.StringIO对象时,如果初始化函数提供了初始化数据,新生成的对象是只读的。所以下面的代码是错误的:s = cStringIO.StringIO("JGood\n"); s.write("OOOKKK");

python

source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents',r'D:\Work'] or somethi
ng like that
# 2. The backup must be stored in a main backup directory
target_dir = '/mnt/e/backup/' # Remember to change this to what you will be usin
g
# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target   =   target_dir   +  time.strftime('%Y%m%d%H%M%S') + '.zip'


if not os.path.exists(today): 
   os.mkdir(today) # make directory 
   print 'Successfully created directory', today


comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
   target = today + os.sep + now + '.zip'
else:
   target = today + os.sep + now + '_' + \
      comment.replace(' ', '_') + '.zip'

 

zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
  print 'Successful backup to', target
else:
  print 'Backup FAILED'

 

 

subprocess再解析

之前已经写过一篇关于Python subprocess的帖子了,subprocess是Python下标准的用于进程创建、通讯的模块,这里再补充一些,注意:我还一直坚守Python2.x,所以不一定适合Python 3。

subprocess简单用法

这是最简单的用法:

p=subprocess.Popen("dir", shell=True)
p.wait()

shell参数根据你要执行的命令的情况来决定,上面是dir命令,就一定要shell=True了,p.wait()可以得到命令的返回值,没有问题。

进程通讯

如果想得到进程的输出,管道是个很方便的方法,这样:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutput,erroutput) = p.communicate()

p.communicate会一直等到进程退出,并将标准输出和标准错误输出返回,这样就可以得到子进程的输出了,上面,标准输出和标准错误输出是分开的,也可以合并起来,只需要将stderr参数设置为subprocess.STDOUT就可以了,这样子:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutput,erroutput) = p.communicate()

如果你想一行行处理子进程的输出,也没有问题:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    buff = p.stdout.readline()
    if buff == '' and p.poll() != None:
        break

死锁

但是如果你使用了管道,而又不去处理管道的输出,那么小心点,如果子进程输出数据过多,死锁就会发生了,比如下面的用法:

p=subprocess.Popen("longprint", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()

longprint是一个假想的有大量输出的进程,那么在我的xp, Python2.5的环境下,当输出达到4096时,死锁就发生了。当然,如果我们用p.stdout.readline或者p.communicate去清理输出,那么无论输出多少,死锁都是不会发生的。或者我们不使用管道,比如不做重定向,或者重定向到文件,也都是可以避免死锁的。

异步subprocess

无论是使用readline还是communicate,这里有个问题是:他们都是同步的,你没有办法在等待子进程输出的同时做点别的什么事情,标准的subprocess是不支持异步和子进程交互的,幸好,幸好,有人提供了Python 3下的异步方法,我移植到Python2.5下面,可以这样用了:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    buff = p.asyncread(timeout=0.5)
    if buff == '' and p.poll() != None:
        break

这里带一个超时去读取子进程的输出,如果超时还没有输出,没关系,父进程可以干点别的什么事情,看起来很棒,subprocess的改动部分代码比较长,不在这里贴了。

关闭
在Python 2.6的subprocess模块中,新增加了一个小的接口就是Terminate,用于进程的终结,可惜可惜,十分遗憾,Windows下,这个Terminate只能杀死subprocess创建的进程,而不能杀死其子进程,如果我们明确知道创建的进程没有子进程,当然可以用这个接口,如果不肯定,则这个接口就没什么用了。

举个简单的例子,如果用shell=True的参数让subprocess创建进程,那么就会多出一个额外的cmd进程,这时用Terminate终结的就是这个cmd进程,而真正那个我们创建的进程则不会被终结。

有很多方法可以对付这个问题,但有一个简单的方法是使用Windows自己提供的taskkill命令,它有一个/T参数,可以杀死一个进程树,正是我们所需要的。subprocess创建的进程有一个pid属性,把pid传给taskkill就ok了。

http://www.liuzhongshu.com/code/subprocess-detail.html

Records:912