如果您想了解Python中使用AES算法的相关知识,那么本文是一篇不可错过的文章,我们将对解决Python2.x和3.x下运行不兼容问题进行全面详尽的解释,并且为您提供关于CentOS升级Python
如果您想了解Python中使用AES算法的相关知识,那么本文是一篇不可错过的文章,我们将对解决Python2.x和3.x下运行不兼容问题进行全面详尽的解释,并且为您提供关于CentOS 升级Python2到Python3并且安装 pip正确方式(解决pip: command not found问题)、iPhone内运行python,返回Killed: 9 ,iOS内安装python2.7等问题的解决方法、Linux下python安装升级详细步骤 | Python2 升级 Python3、pycharm2016 ,python 3.5 , django 1.11 环境不兼容问题的解决的有价值的信息。
本文目录一览:- Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)(python3 aes)
- CentOS 升级Python2到Python3并且安装 pip正确方式(解决pip: command not found问题)
- iPhone内运行python,返回Killed: 9 ,iOS内安装python2.7等问题的解决方法
- Linux下python安装升级详细步骤 | Python2 升级 Python3
- pycharm2016 ,python 3.5 , django 1.11 环境不兼容问题的解决
Python中使用AES算法(解决Python2.x和3.x下运行不兼容问题)(python3 aes)
两年前使用python时,碰到2.x下AES加密解密算法代码无法在3.x下顺利运行,花点时间解决了兼容问题,在2.7、3.6、3.7下运行良好。
Linux系统安装依赖库比较简单,Windows下稍嫌繁琐,装完必须的库也可以正常运行。
代码整理如下:
# -*- coding: utf-8 -*-
import base64
import sys
from Crypto import Random
from Crypto.Cipher import AES
# Author: areful
# Date: 2017-07-06
class AESCipher:
def __init__(self, key, iv=Random.new().read(AES.block_size)):
self.key = key
self.iv = iv
self.mode = AES.MODE_CBC
if sys.version > ''3'':
self.PY3 = True
else:
self.PY3 = False
def encrypt(self, text):
if self.PY3:
return self.encrypt36(text)
else:
return self.encrypt27(text)
def encrypt27(self, text):
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
bs = AES.block_size
text_length = len(text)
amount_to_pad = bs - (text_length % bs)
if amount_to_pad == 0:
amount_to_pad = bs
pad = chr(amount_to_pad)
text1 = text + pad * amount_to_pad
cipher_text = cipher.encrypt(text1)
return base64.b64encode(cipher_text)
def encrypt36(self, text):
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
bs = AES.block_size
text_length = len(text.encode(''utf-8''))
amount_to_pad = bs - (text_length % bs)
if amount_to_pad == 0:
amount_to_pad = bs
pad = chr(amount_to_pad)
text1 = text + pad * amount_to_pad
cipher_text = cipher.encrypt(text1)
encrypted_str = str(base64.b64encode(cipher_text), encoding=''utf-8'')
return encrypted_str
def decrypt(self, text):
import base64
base_text = base64.b64decode(text)
cipher = AES.new(self.key, self.mode, self.iv)
plain_text = cipher.decrypt(base_text).decode(''utf-8'')
pad = ord(plain_text[-1])
ne = plain_text[:-pad]
return ne
@staticmethod
def pad(s):
bs = AES.block_size
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
@staticmethod
def unpad(s):
return s[0:-ord(s[-1])]
@staticmethod
def __pad(text):
text_length = len(text)
amount_to_pad = AES.block_size - (text_length % AES.block_size)
if amount_to_pad == 0:
amount_to_pad = AES.block_size
pad = chr(amount_to_pad)
return text + pad * amount_to_pad
@staticmethod
def __unpad(text):
pad = ord(text[-1])
return text[:-pad]
@staticmethod
def test(key, iv, text):
cipher = AESCipher(key, iv)
encrypted_msg = cipher.encrypt(text)
print(encrypted_msg)
msg = cipher.decrypt(encrypted_msg)
print(msg)
if __name__ == ''__main__'':
AESCipher.test(''ABCDEFGHICKLMNOP'',
bytes(bytearray(b''\x01\x02\x03\x04\x05\x06\x07\x08\x41\x42\x43\x44\x45\x46\x47\x48'')),
''areful.测试文本'')
CentOS 升级Python2到Python3并且安装 pip正确方式(解决pip: command not found问题)
下面是小编 jb51.cc 通过网络收集整理的代码片段。小编小编现在分享给大家,也给大家做个参考。
CentOS 7 中默认安装了 Python,版本比较低(2.7.5),为了使用新版 3.x,需要对旧版本进行升级。由于很多基本的命令、软件包都依赖旧版本,比如:yum。所以,在更新 Python 时,建议不要删除旧版本(新旧版本可以共存)。
一、查看当前 python 版本
[root@ansible ~]# python -V
Python 2.7.5
二、下载新的 python 包并安装
进入 python 官网(https://www.python.org),选择需要的版本。此处我选择当前最新版本 python3.6.1
[root@ansible soft]#yum install gcc gcc-c++ -y
[root@ansible soft]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
[root@ansible soft]# tar xvf Python-3.6.1.tgr
[root@ansible soft]# cd Python-3.6.1/
[root@ansible Python-3.6.1]# ./configure
[root@ansible Python-3.6.1]# make
[root@ansible Python-3.6.1]#make install
三、验证
#python -V #一个是旧版本,一个是新版本
Python 2.7.5
# python3 -V
Python 3.6.1
四、设置 3.X 为默认版本
查看 Python 的路径,在 /usr/bin 下面。可以看到 python 链接的是 python 2.7,所以,执行 python 就相当于执行 python 2.7。
[root@ansible ~]# ls -al /usr/bin | grep python
-rwxr-xr-x. 1 root root 11232 Dec 2 2016 abrt-action-analyze-python
lrwxrwxrwx. 1 root root 7 May 26 2017 python -> python2
lrwxrwxrwx. 1 root root 9 May 26 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 6 2016 python2.7
将原来 python 的软链接重命名:
# mv /usr/bin/python /usr/bin/python.bak
将 python 链接至 python3:
# ln -s /usr/local/bin/python3 /usr/bin/python
五、配置 yum
升级 Python 之后,由于将默认的 python 指向了 python3,yum 不能正常使用,需要编辑 yum 的配置文件,此时:
[root@ansible-admin Python-3.6.1]# yum list
File "/usr/bin/yum",line 30
except KeyboardInterrupt,e:
SyntaxError: invalid Syntax
修改/usr/bin/yum 和/usr/libexec/urlgrabber-ext-down,将 #!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可。
# vim /usr/bin/yum
# vim /usr/libexec/urlgrabber-ext-down
六、配置 pip
如果 pip list 时出现问题:
# pip list
-bash: pip: command not found
执行:
yum install epel-release
yum install -y python-pip
以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。
iPhone内运行python,返回Killed: 9 ,iOS内安装python2.7等问题的解决方法
连接10.2系统版本的越狱iPhone,通过Cydia下载python,想在手机iPhone内运行python,但是在输入python后会返回 Killed: 9 的问题。
通过Cydia下载的python是2.5版本的,版本不够新,会有很多不便之处。
Killed: 9 这个问题是因为签名问题导致的。
于是用 ldid -S /usr/bin/python 重签,输入python 还是返回 Killed: 9
不管是python版本问题还是python无法正常在iPhone里运行。
我们来重新安装python
首先删除iPhone内已经存在的python
然后点击 下面的地址进行下载:
http://python.mila432.com/python_2.7.13-1_iphoneos-arm.deb
或者百度网盘下载:
链接: https://pan.baidu.com/s/1bQpGuU 密码:fhqq
将下载好后的 python_2.7.13-1_iphoneos-arm.deb 通过scp 传到iPhone里
连接到iPhone ,通过dpkg 安装python
然后对python进行签名:
ldid -S /usr/bin/python
ios10+:
ldid -S /usr/lib/libpython2.7.dylib
ldid -S /usr/lib/libssl.1.0.0.dylib
ldid -S /usr/lib/libcrypto.1.0.0.dylib
how to install pip:
PYTHONHTTPSVERIFY=0 easy_install pip
Linux下python安装升级详细步骤 | Python2 升级 Python3
Linux下python升级步骤 Python2 ->Python3
多数情况下,系统自动的Python版本是2.x
或者yum直接安装的也是2.x
但是,现在多数情况下建议使用3.x
那么如何升级呢?-ps:我用的root权限
下面详细讲解升级步骤;
首先下载源tar包
可利用linux自带下载工具wget下载,如下所示:
wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz
下载完成后到下载目录下,解压
tar -xzvf Python-3.3.0.tgz
进入解压缩后的文件夹
cd Python-3.3.0
在编译前先在/usr/local建一个文件夹python3(作为python的安装路径,以免覆盖老的版本)
mkdir /usr/local/python3
开始编译安装
./configure --prefix=/usr/local/python3
make
make install
此时没有覆盖老版本,再将原来/usr/bin/python链接改为别的名字
mv /usr/bin/python /usr/bin/python_old2
再建立新版本python的链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python
这个时候输入
python -V
就会显示出python的新版本信息
[idolaoxu@localhost home]# python -V
Python 3.3.0
PS:如果不建立新安装路径python3,而是直接默认安装,则安装后的新python应该会覆盖linux下自带的老版本,也有可能不覆盖,具体看安装过程了,
这个大家可以自己试验下,当然如果还想保留原来的版本,那么这种方法最好不过了。
最后扩充下,
这种方法虽然能安装成功,但是它带来了新的问题,比如yum不能正常用了
修改/usr/bin/yum的第一行为:
#!/usr/bin/python_old2
就可以了
如上是讲解已经存在Python2的情况下如何升级,如果是第一次安装呢?那更简单,有些步骤直接可省去,直接安装就OK
pycharm2016 ,python 3.5 , django 1.11 环境不兼容问题的解决
之前,python2.7 的和 django1.9 , 同步数据库,在 pycharm2016 下面 运行都可以没有什么问题,
后面发现问题,python 3.5 和 django1.9 居然不配套,, 可以在 pycharm 下运行,
但是,不能用 django 的命令同步数据库,
python manage.py makemigrations
python manage.py migrate
很郁闷, 最后更新到最新的 1.11
pip unstall django (卸载 1.9 版本的)
pip unstall django (安装最新的 1.11)
更新 1.11 后 可以 同步数据库,
但是,pycharm2016 在 run 的 configure 的时候 提示 python 3.5 和 当前的 django 环境不兼容,
只有降级到 django1.10 在 pycharm 才 没有报错了 ,问题彻底解决, 估计 python3.6 能和 1.11 兼容吧。
所以 这个 路只有走过了,才知道有多少坑 。
关于Python中使用AES算法和解决Python2.x和3.x下运行不兼容问题的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于CentOS 升级Python2到Python3并且安装 pip正确方式(解决pip: command not found问题)、iPhone内运行python,返回Killed: 9 ,iOS内安装python2.7等问题的解决方法、Linux下python安装升级详细步骤 | Python2 升级 Python3、pycharm2016 ,python 3.5 , django 1.11 环境不兼容问题的解决等相关内容,可以在本站寻找。
本文标签: