【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:\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]: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM