python小工具

1:启动一个下载服务器:方便传输大量文件

1:python2使用的模块
 ubuntu@ubuntu:~/share/python_Linux$ python2.7  -m SimpleHTTPServer
    
2:python3使用的模块 
ubuntu@ubuntu:~/share/python_Linux$ python3.7 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

3:访问
ubuntu@ubuntu:~$ ip addr show  #查看本机ip地址为192.168.43.97
http://192.168.43.97:8000/   #浏览器中输入ip地址和端口号即可访问下载

2:字符窜转换为JSON对象

#底层服务的API一般以json的格式返回,为了方便debug,会将json转换为字符串记录到日志文件中,当需要分析问题时,就要将日志中的json字符串转换为json对象,以提高日志的可读性.
'{"employees": [{"firstName": "Bill","lastName": "Gates"},{"firstName": "George","lastName": "Bush"},{"firstName": "Thomas","lastName": "Carter"}]}'


ubuntu@ubuntu:~$ echo '{"employees": [{"firstName": "Bill","lastName": "Gates"},{"firstName": "George","lastName": "Bush"},{"firstName": "Thomas","lastName": "Carter"}]}' | python -m json.tool

{
    "employees": [
        {
            "firstName": "Bill",
            "lastName": "Gates"
        },
        {
            "firstName": "George",ubuntu@ubuntu:~$ python
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json

            "lastName": "Bush"
        },
        {
            "firstName": "Thomas",
            "lastName": "Carter"
        }
    ]
}

3:检查第三方库是否已经安装

ubuntu@ubuntu:~$ python3
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
>>> 




1:方法1:python交互终端import 库名
ubuntu@ubuntu:~$ python
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json

2:方法2:使用python解释器的-c参数快速执行import语句
ubuntu@ubuntu:~/share/python_Linux$ python -c "import string"
ubuntu@ubuntu:~/share/python_Linux$ python -c "import traceback" 
#可以写脚本批量验证

参考书籍:《Python Linux系统管理与运维》---赖明星

你可能感兴趣的:(python小工具)