linux安装Python 3.6.2 SSL

2017-09-20

Python 3.6安装过程。

1、下载
cd /opt
wget -c https://www.wallcopper.com/tar/Python-3.6.2.tgz
wget -c https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
2、编译安装
tar zxf Python-3.6.2.tgz
cd Python-3.6.2
./configure --prefix=/usr/local/python3.6 --enable-optimizations;
make;make install
If you want a release build with all stable optimizations active (LTO,PGO, etc),
please run ./configure --enable-optimizations
这时才会创建 Python-3.6.2/Module/Setup
3、默认安装路径
/usr/local/python3.6/bin
4、ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python
5、毕竟丰富的第三方库是python的优势所在,为了更加方便的安装第三方库,使用pip命令,我们需要进行相应的安装。
6、安装pip前需要前置安装setuptools

命令如下:
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar zxf setuptools-19.6.tar.gz
cd setuptools-19.6
python3 setup.py build
python3 setup.py install

  报错: RuntimeError: Compression requires the (missing) zlib module
  我们需要在linux中安装zlib-devel包,进行支持。
  yum install zlib-devel
  重新安装setuptools
7、安装pip命令:
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar zxf pip-8.0.2.tar.gz
cd pip-8.0.2
python3 setup.py build
python3 setup.py install
如果没有意外的话,pip安装完成。
8、pip3 install paramiko requests

  报这个错
  pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
  然后开始进行如下操作
  yum install openssl
  yum install openssl-devel
tar zxf openssl-1.0.1k.tar.gz
cd openssl-1.0.1k
./config --openssldir=/usr/local/ssl -fPIC
make depend
make
make install

SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
cd Python-3.6.2
./configure --prefix=/usr/local/python3.6 --enable-optimizations
vi Python-3.6.2/Module/Setup
make -j8
make altinstall
pip install pep8
pip list

 
9、测试requests:
wget --no-check-certificate https://pypi.python.org/packages/b0/e1/eab4fc3752e3d240468a8c0b284607899d2fbfb236a56b7377a329aa8d09/requests-2.18.4.tar.gz
tar zxf requests-2.18.4.tar.gz
cd requests-2.18.4
python3.6 setup.py build
python3.6 setup.py install
pip3.6 list

#!/usr/bin/python3.6

import urllib.request
import json
import sys

Corpid="id"
Corpsecret="secretid"

# get AccessToken
def gettoken(corp_id,corp_secret):
gettoken_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret
try:
token_file = urllib.request.urlopen(gettoken_url)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
token_json.keys()
token = token_json['access_token']
return token

#send msg
accesstoken = gettoken( Corpid , Corpsecret );
#msg = senddata(accesstoken,'notifystr');
print(accesstoken);

10、测试requests
#! /usr/bin/python3.6

import requests
import json

Corpid="abc"
Corpsecret="a-aJko9MSzzpKo"

def get_token():

url='https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid':Corpid,
'corpsecret':Corpsecret,
}
req = requests.post(url, params=values)
data = json.loads(req.text)
return data["access_token"]

def send_msg():
url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+get_token()
values = """{
"touser":"wallcopper",
"msgtype":"text",
"agentid":"1",
"text":{"content": "%s"
},
"safe":"0"
}""" %(str("10.1.1.8 is down"))

data = json.loads(values)
req = requests.post(url, values)

if __name__ == '__main__':
send_msg()
print("ok")
11、python3.0不支持file函数
http://python3porting.com/differences.html#file
例如,f=file(data_pkl,'rb')
可以使用open()替代,也可以使用io.IOBase。
12、python3.0不再支持urllib2
ImportError: No module named 'urllib2'
response = urllib2.urlopen(req)
+ response = urllib.request.urlopen(req)

分类:Linux | 标签: |

相关日志

评论被关闭!