1.需求

为了保证线上代码安全和效率,使用python编写代码,pyc可直接反编译,于是把重要代码编译so文件

2.工作

2.1 安装相关库:

pip install cython
yum install -y gcc python-devel

2.2 创建setup.py

#!/usr/bin/env python
# coding=utf-8
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("xxxx.py")
)

2.3 运行

root@868aec72d1a4:/xxx/xxx/xxx# python setup.py build_ext
Compiling xxx.py because it changed.
[1/1] Cythonizing xxx.py
running build_ext
building 'lib.xxx' extension
creating build
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c xxx.c -o build/temp.linux-x86_64-2.7/xxx.o
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/lib
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/xxx.o -o build/lib.linux-x86_64-2.7/lib/xxx.so

会在当年目录上传build目录,结构如下:

root@868aec72d1a4:/spider/codes/lib# tree build/
build/
|-- lib.linux-x86_64-2.7
| `-- lib
| `-- xxx.so
`-- temp.linux-x86_64-2.7
`-- xxx.o 3 directories, 2 files

2.4 获取so文件即可

使用cython把python编译so的更多相关文章

  1. Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑

    Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑 前言 基于 python27 的 pyc 很容易被反编译,于是想到了pyd,加速运行,安全保护 必要准备 安装 ...

  2. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

  3. Cython保护Python代码

    注:.pyc也有一定的保护性,容易被反编译出源码... 项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护.这一方法,虽仍能被反编译,但难度会 ...

  4. 2.准备Python编译环境

    2.准备Python编译环境 2.1下载Python2.7.6.tgz.ipython1.2.1.tgz.sqlite-autoconf-3071401.tar.gz 2.2安装Python2.7.6 ...

  5. 【转】ubuntu下安装eclipse以及配置python编译环境

    原文网址:http://blog.csdn.net/wangpengwei2/article/details/17580589 一.安装eclipse 1.从http://www.eclipse.or ...

  6. 使用Code::Blocks配置Python编译环境

    1.先在CodeBlock中新建C或C++工程. CodeBlock新建工程步骤:File——New——Project——Console applications——C或C++都可——Project所 ...

  7. 关于python编译的一点小结

    大家都知道python是脚本语言,源码可以直接执行,有时需要提高执行效率或者保密(因为有时候不想让使用人看到源码文件),那就涉及到python编译了,那么该如何做呢? 有两种方法可以做到. 1.一种是 ...

  8. python 编译源文件

    背景 近期项目到了部署的阶段.由于项目后台和算法都是用Python "撸的",但是又不希望将源代码直接 "release" 到 “客户”哪里.于是开始思考... ...

  9. python编译pyc工程--导包问题解决

    利用python 编译工程,生产pyc文件 pyc文件好处:是一种二进制机器码,并且隐藏了源文件代码,但是有和py文件一样的功能(可以理解为效果一样) 所以可以将代码隐藏,便于商业价值,保护代码隐私还 ...

随机推荐

  1. MyBatis之xml配置配置文件(一)(xml配置文件)

    一.properties标签 <properties resource="jdbc.properties"></properties> 使用properti ...

  2. (python)循环中动态产生变量

    >>> for i in xrange(5): exec 'a'+str(i)+' = '+str(i)+'' >>> a0 0 >>> a1 1 ...

  3. c3p0 数据库连接池相关知识

    c3p0数据库连接池的配置文件放在eclipse的src目录下,代码就可以识别. c3p0的配置文件的内容如下: <!-- Uncomment and set any of the option ...

  4. Memcache面试题

    * memcached是怎么工作的? * memcached最大的优势是什么? * memcached和MySQL的query cache相比,有什么优缺点? * memcached和服务器的loca ...

  5. substr 方法

    substr 方法 返回一个从指定位置开始,并具有指定长度的子字符串. 参数 start 必选.所需的子字符串的起始位置.字符串中第一个字符的索引为 0. length 可选项.返回的子字符串中包含的 ...

  6. 17-THREE.JS 光晕滤镜

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  7. SVN中如何为文件夹中的所有文件加锁

    经过一段时间的试用,发现不加锁的共享式开发还是不太方便.还是全部设置为独占式加锁,如有共享式修改需求再设置为不加锁比较好. 经过一番摸索,总结出如下的加锁方式是可行的: 注:第一步是必须的,必须完成第 ...

  8. 浪漫爱心--第三方开源--PeriscopeLayout

    点此下载 使用很简单,首先在xml里面添加 <Button android:id="@+id/btn_start" android:layout_width="wr ...

  9. c++primer 第二章编程练习答案

    2.7.1 #include<iostream> int main() { using namespace std; ]; ]; cout << "input nam ...

  10. XXX.APP已损坏,打不开.你应该将它移到废纸篓

    XXX.APP已损坏,打不开.你应该将它移到废纸篓 MACOS 10.12 SIERRA 如遇:「xxx.app已损坏,打不开.你应该将它移到废纸篓」,并非你安装的软件已损坏,而是Mac系统的安全设置 ...