当前位置: 移动技术网 > IT编程>脚本编程>Python > 【Python 开发】第三篇:python 实用小工具

【Python 开发】第三篇:python 实用小工具

2018年09月19日  | 移动技术网IT编程  | 我要评论

www.0578.cm,芙罗丽卡罗奥尼达,我家的天使演员表

一、快速启动一个web下载服务器

官方文档:

1)web服务器:使用simplehttpserver,快速帮我们共享指定目录下内容。

  • 各种linux发行版通常都内置了python,故使用此方法非常方便。在其它os(比如windows)此方法也有效,但是要麻烦一些,必须先搭建python环境。
  • simplehttpserver是python 2自带的一个模块,是python的web服务器。它在python 3已经合并到http.server模块中。

simplehttpserver有一个特性:

  • 如果待共享的目录下有,那么文件会被视为默认主页;
  • 如果不存在文件,那么就会显示整个目录列表。

2)simplehttpserver使用方法:

  1)进入待分享的目录
  2)执行命令python -m simplehttpserver 端口号
   注意:不填端口号则默认使用8000端口。
  3)浏览器访问该主机的地址:http://ip:端口号/

示例:执行命令

在python2.x中:
[root@host130 ~]# python -m simplehttpserver
serving http on 0.0.0.0 port 8000 ...
192.168.31.86 - - [18/sep/2018 21:17:53] "get / http/1.1" 200 -

也可以指定某个端口;
[root@yanglt tmp]# python -m simplehttpserver 80
serving http on 0.0.0.0 port 80 ...
118.199.86.147 - - [18/sep/2018 21:43:25] "get / http/1.1" 200 -

 

在python3.x中:
[root@host130 ~]# python -m http.server  
serving http on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
192.168.31.86 - - [18/sep/2018 21:19:15] "get / http/1.1" 200 -

该下载服务器,默认端口号是8000
打开网页输入:http://192.168.31.77:8000

 二、python 的包管理工具pip

官网指导:
1、pip简介:

为了方便用户安装和管理第三方库和软件(ruby的gem,nodejs的npm),python也有自己的包管理工具pip。
默认情况下,python 2.7.9+和python 3.4以上的版本则内置了pip,无需安装直接可以使用。
如果没有安装,操作如下:

[root@host130 ~]# yum -y install python-pip
新版本升级:
[root@host130 ~]# pip install -u pip

2、pip的优点有:
pip的优点:
1)提供丰富的功能,其中竞争对手easy_install 则只支持安装,没有提供卸载和显示已安装列表的功能;
2)能够很好的支持虚拟化环境;
3)可以通过requirements.txt集中管理依赖
4)能够处理二进制格式(.whl)
5)是先下载够安装,安装失败会清理干净,不会留下中间状态

3.pip常用命令:

1)查找安装包
[root@host130 tmp]# pip search flask
flask-orientdb (0.1)        - a flask extension for using

2)安装特定的安装包版本
[root@host130 ~]# pip install flask==0.8
collecting flask==0.8

3)删除安装包
[root@host130 ~]# pip uninstall werkzeug
uninstalling werkzeug-0.14.1:

4)查看安装包信息
[root@host130 ~]# pip show flask
name: flask
version: 0.8
summary: a microframework based on werkzeug, jinja2 and good intentions
home-page: http://github.com/mitsuhiko/flask/
author: armin ronacher
author-email: armin.ronacher@active-4.com
license: bsd
location: /usr/lib/python2.7/site-packages
requires: werkzeug, jinja2
required-by: 
[root@host130 ~]# 

5)检查安装包的依赖是否完整
[root@host130 ~]# pip check flask
flask 0.8 requires werkzeug, which is not installed.
[root@host130 ~]#
6)查看已安装的安装包列表
[root@host130 ~]# pip list

7)导出已安装的安装包列表到requirements文件
[root@host130 ~]# pip freeze > requirements.txt

8)从requirement文件安装
[root@host130 ~]# pip install -r requirements.txt 

9)使用pip补全
[root@host130 ~]# pip completion --bash >> ~/.profile
[root@host130 ~]# source ~/.profile 

4、pip的镜像源

国内源:
新版ubuntu要求使用https源,要注意。

清华:

阿里云:

中国科技大学: 

华中理工大学:

山东理工大学:

豆瓣:

1)使用第三方源,如豆瓣和阿里云的源加速软件安装
[root@host130 ~]# pip install -i http://pypi.douban.com/simple/ flask
looking in indexes: http://pypi.douban.com/simple/

2)每次用i指定加速源比较麻烦。
linux下,修改linux下,修改 ~/.pip/pip.conf(没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:
[root@host130 ~]# mkdir -p  ~/.pip/
[root@host130 ~]# vim ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

windows下:直接在user目录中创建一个pip目录,如:c:\users\xx\pip,新建文件pip.ini。内容同上。


3)下载到本地安装

 三、python编辑器ipython

需要解决python2.7和python3.x兼容问题
[root@host130 ~]# yum -y install ipython   #当然也可以通过[root@host130 ~]# pip install ipython
[root@host130 ~]# which ipython
/usr/bin/ipython
[root@host130 ~]# 
[root@host130 ~]# ipython
python 2.7.5 (default, nov  6 2016, 00:28:07) 
type "copyright", "credits" or "license" for more information.

ipython 3.2.1 -- an enhanced interactive python.
?         -> introduction and overview of ipython's features.
%quickref -> quick reference.
help      -> python's own help system.
object?   -> details about 'object', use 'object??' for extra details.

in [1]: 

我们看到默认进入的是python2.x

解决方法:

[root@host130 ~]# which ipython   #找到ipython的命令位置
/usr/bin/ipython
[root@host130 ~]cd /usr/bin/
[root@host130 bin]# cp ipython ipython3
[root@host130 bin]# vim ipython3
[root@host130 bin]# cat ipython3
#!/usr/bin/python3   #将原来的python2改为现在的python3
# this script was automatically generated by setup.py
if __name__ == '__main__':
    from ipython import start_ipython
    start_ipython()
[root@host130 bin]# 
[root@host130 ~]# ipython3   #再次启动成功
python 3.7.0rc1 (default, sep 18 2018, 18:22:18) 
type 'copyright', 'credits' or 'license' for more information
ipython 6.5.0 -- an enhanced interactive python. type '?' for help.

in [1]: 

此时python2和python3同时存在:
[root@host130 ~]# ipython
python 2.7.5 (default, nov  6 2016, 00:28:07) 
type "copyright", "credits" or "license" for more information.

ipython 3.2.1 -- an enhanced interactive python.
?         -> introduction and overview of ipython's features.
%quickref -> quick reference.
help      -> python's own help system.
object?   -> details about 'object', use 'object??' for extra details.

in [1]: 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网