zoukankan      html  css  js  c++  java
  • 【Python 开发】第三篇:python 实用小工具

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

    官方文档:https://docs.python.org/2/library/simplehttpserver.html

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

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

    SimpleHTTPServer有一个特性:

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

    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

    官网指导:https://pip.pypa.io/en/latest/user_guide/
    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源,要注意。

    清华:https://pypi.tuna.tsinghua.edu.cn/simple

    阿里云:https://mirrors.aliyun.com/pypi/simple/

    中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/

    华中理工大学:https://pypi.hustunique.com/

    山东理工大学:https://pypi.sdutlinux.org/

    豆瓣:https://pypi.douban.com/simple/

    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:Usersxxpip,新建文件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]: 
  • 相关阅读:
    吴军博士《浪潮之巅》
    第十二周
    第十一周
    第十周
    第九周
    第四次作业
    第四周
    学习进度表
    世界是数字的
    第二阶段团队第八天成果。
  • 原文地址:https://www.cnblogs.com/yangleitao/p/9671585.html
Copyright © 2011-2022 走看看