涉及到socket通信,果然还是linux比较稳定啊。
所以今天记录一下python在CentOS上的部署步骤。
python2安装相关
linux自带的python2.7,只是没有pip,单独装pip的方法如下。
# 下载安装脚本 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py # 运行安装脚本 sudo python get-pip.py
装好之后,应该就可以直接pip调用了。
如果无法调用,则找到pip安装位置,可能是/usr/local/python/bin,建立个软连接。
ln /usr/local/python/bin/pip /usr/local/bin/pip
python3安装过程
python3需要下载安装包编译安装,安装流程如下。
# 安装相应的编译工具 yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum install -y libffi-devel zlib1g-dev yum install zlib* -y # 下载安装包 wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz # 解压 tar -xvJf Python-3.7.2.tar.xz # 创建编译安装目录 mkdir /usr/local/python3 # 编译安装 cd Python-3.7.2 ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl # 第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便. # 第二个可以提高python10%-20%代码运行速度. # 第三个是为了安装pip需要用到ssl,后面报错会有提到. make && make install # 创建软链接 ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
然后就可以使用python3来运行python脚本了。
pip换源
因为一些原因,pip下载安装速度极慢(F**K U G*W),所以国内使用不得不换国内源。
临时使用
使用pip的时候在后面加上-i参数,指定pip源
pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple
永久修改
linux,修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple
windows,在user目录(%HOMEPATH%)中创建一个pip目录,如:C:\Users\administrator\pip,在pip 目录下新建文件pip.ini,内容如下
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn timeout = 6000
其中trusted-host意思是信任这个地址,可以免去ssl验证。
常用国内镜像源:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
pip设置代理
有三种常用方式:
1.单次设置
pip install pyqt5 --proxy=代理服务器IP:端口号
2.临时设置(重连后失效)
export http_proxy='http://代理服务器IP:端口号 export https_proxy='http://代理服务器IP:端口号'
3.永久设置
vim /etc/profile: export http_proxy='http://代理服务器IP:端口号' export https_proxy='http://代理服务器IP:端口号' source /etc/profile
其中需要注意的是,后两种方法是通过设置环境变量的方法设置代理,这个环境变量其他类似程序也会调用,因此也可能受到影响。
python脚本后台运行
nohup python -u test.py > nohup.log 2>&1 &
含义解释:
nohup 不挂起的意思
-u 代表程序不启用缓存,也就是把输出直接放到log中,没这个参数的话,log文件的生成会有延迟
> nohup.log 将输出日志保存到这个log中
2>1 2与>结合代表错误重定向,而1则代表错误重定向到一个文件1,而不代表标准输出;
2>&1 换成2>&1,&与1结合就代表标准输出了,就变成错误重定向到标准输出.
& 最后一个& ,代表该命令在后台执行
命令运行后会有提示,示例:
[1] 4203
代表进程4203中运行。
运行完毕后不要直接关闭连接窗口,尽量使用exit命令退出,否则nohup可能假死睡眠。
查看nohub命令下运行的所有后台进程:
jobs
查看后台运行的所有进程:
ps -aux
查看后台运行的所有python 进程:
ps -aux |grep python
删除进程
kill -9 [进程id]
-9 的意思是强制删除
你一定得认识到自己想往哪个方向发展,
然后一定要对准那个方向出发,
要马上。
你再也浪费不起多一秒的时间了,
你浪费不起。
——《麦田里的守望者》
评论
821765 145184I like this web site extremely considerably so significantly superb info . 697828
359490 420822Its really a cool and useful piece of data. Im glad that you shared this helpful information with us. Please maintain us informed like this. Thanks for sharing. 387191
676397 739520Safest the world toasts are produced to captivate and also faithfulness to your wedding couple. Beginner sound system watching high decibel locations would be wise to always remember some sort of vital secret created by presentation, which is your auto. very best man speeches funny 776320
538541 548337Certain paid google internet pages offer complete databases relating whilst individual essentials of persons even though range beginning telephone number, civil drive public records, as nicely as criminal arrest back-ground documents. 29322
276659 401806I likewise conceive so , perfectly written post! . 567554
116533 184837Right after I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get four emails with exactly the same comment. Is there any method youll be able to take away me from that service? Thanks! 147829
If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state.
https://www.zuj.edu.jo/video-promo-al-zaytoonah-university-of-jodran/