Python:基础入门大纲

python2.7

安装

  • 安装目录: /usr/local/python2.7
  • 包管理: pip
  • 软链接至path目录:
    ln -s /usr/local/python2.7/bin/python /usr/bin/python2.7
    ln -s /usr/local/python2.7/bin/pip /usr/bin/pip2.7

基础

数据结构

  • 字符串
  • 数字
  • 列表
  • 元组
  • 字典

循环

  • for
s = 0
for i in xrange(10):
    s += i
print(s)
  • while
s, i = 0, 1
while i <10:
    s += i
    i += 1
print(s)
  • continue, break
shell一致

分支

真、假
真: True, 非空值, 非零值
假: False, 空值(“”), 零值(0, 0.0, [], {}, (), set()), None

if (条件为真):
    ...
else:
    ...
if ( a and b or c 为真):
    ...
elif (条件为真):
    ...
elif (条件为真):
    ...
else:
    ...

规范

头部

#!/bin/env python2.7
#coding: utf8  
#desc: 脚本说明
#author: 创建人
#editor: 更新人

如脚本需支持中文,使用以下两种办法:
python3无此问题,可以忽略

# python2
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# python2
var = u"中文"

命名规范


  • 大写驼峰式,如 FcboxServer()
  • 函数名
    小写+下划线,如 get_fcbox_server()
  • 包名,文件名
    小写+下划线,如 fcbox_python.py
  • 局部变量名
    小写+下划线,如 fcbox_server=”localhost”
  • 全局变量,常量
    大写+下划线,如 PROCESS_NUM=4

可执行入口

if __name__ == "__main__":
    main()

基础库

系统操作:

  • os
import os 

# 环境变量
print(os.environ)
print(os.environ["PATH"])
os.environ["NAME"] = "FCBOX"
print(os.environ["NAME"])

# 更改目录
os.chdir("/tmp")
os.getcwd()

# ls
os.listdir("/app")
# 创建目录
os.mkdir("/app")
# 创建多级目录
os.mkdirs("/app/testapp")

# 删除文件
os.remove("/tmp/test")

# 删除空目录
os.rmdir("/tmp/test")
os.removedirs("/tmp/test")

# 判断文件
os.path.isfile("/tmp")
# 判断目录
os.path.isdir("/tmp")
# 绝对路径
os.path.abspath(__file__)
# 目录名
os.path.dirname(__file__)
# 文件名
os.path.basename(__file__)
  • shutil
import shutil

# 移动文件
shutil.move("/tmp/test1", "/tmp/test2")
# 复制文件
shutil.copy("/tmp/test1", "/tmp/test2")     # cp 
shutil.copy2("/tmp/test1", "/tmp/test2")    # cp -p
# 删除目录
shutil.rmtree("/tmp/test")

文件读写

  • open
    文件打开模式:
    r: 只读
    w: 只写(覆盖原内容)
    w+: 读写(覆盖原内容)
    a: 追加只写(不覆盖原内容)
    a+: 追加读写(不覆盖原内容)

    写模式时,若文件不存在会自动创建
# 一次读取文件
f = open("test.txt", r)
print(f.read())
f.close()

with open("test.txt", r) as f:
    print(f.read())

# 按行读取文件
with open("test.txt", "r") as f:
    for line in f:
        print(line.strip())

#写入文件
content = "fcbox\npython\n"
with open("test.txt", "w") as f:
    f.write(content)

日志

  • logging
import logging
# 直接使用,输出至终端
logging.info("终端日志")
# 简单配置,输出至文件
# 定义日志器配置
logging.basicConfig(
    filename="test.log",        # 日志文件
    filemode="a",               # 追加文件模式
    format="%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.INFO
)
logging.info("文件日志")

json序列化、反序列化

  • json
import json
# 序列化, python字典对象转化为json
dict_test = {"name": "王", "company": "aa", "position": "op"}

print(json.dumps(
    dict_test,
    ensure_ascii=False  # 有中文内容时,需添加此参数
))

# 序列化, 并输出至json文件
json.dump(
    dict_test, 
    open("test.json", "w"), 
    ensure_ascii=False  # 有中文内容时,需添加此参数
)
# 反序列, 读取json转换为python字典对象
json_test = '{"name": "王", "company": "aa", "position": "op"}'
print(json.loads(json_test))

字符处理,正则

  • split
# 拆分字符串
string = "host1,host2,host3"
list_str = string.split(",")
print("%s 第一个元素为: %s" % (list_str, list_str[0]))
  • replace
string = "host1,host2,host3"
new_string = string.replace("host", "server")
print(new_string)

new_string2 = string.replace("host", "server", 1)
print(new_string2)
  • strip
# strip 去除两端指定字符,默认为空白符(空格,换行)
string = "  fcbox\n"
print(string.strip())

# lstrip 去除左端指定字符,默认为空白符(空格,换行)
print(string.lstrip())

# rstrip 去除右端指定字符,默认为空白符(空格,换行)
print(string.rstrip())
  • re
import re

# 编译正则对象, 可复用
reg = re.compile("^[0-9]{4,}.*")
# 编译后可以例用search(), match()方法

# 搜索, 搜索整个字符串
reg = re.compile("BOX[0-9]{4,}", re.I)
result = reg.search("Fcbox2018shenzhen")
print(result)

# 匹配, 从开头开始匹配
reg = re.compile("box[0-9]{4,}")
result = reg.match("Fcbox2018shenzhen")
print(result)

reg2 = re.compile("^[a-zA-Z]{2}box([0-9]{4,})")
result2 = reg2.match("Fcbox2018shenzhen")
print(result2.group(1))

# 替换
old = "fcbox-2017"
new = re.sub("[0-9]{4}$", "2018", old)
print(new)

随机数

  • time 时间戳
import time
randnum = int(time.time())
print(randnum)
  • random 绝对随机
import random
# 指定范围随机数
print(random.randint(100, 1000))
# 从指定字符中选取随机
randstr = "abcdefg123456789"
print(random.choice(randstr))

# 指定长度的随机数字
def randstring(length):
    raw = "abcdefg0123456789"
    s = ""
    for i in xrange(abs(length)):
        s += random.choice(raw)
    return s

print(randstring(10))

常用库

命令调用

  • subprocess
import subprocess
# 调用命令,阻塞等待, 打印运行输出
cmd = "ping fcbox.com -w 1 -n 3"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# p.poll() 检查是否子进程是否运行完成,取得返回码
while p.poll() is None:     
    print(p.stdout.readline().strip())
print(p.poll())

# 调用命令,获取运行返回码
cmd = "ping fcbox -w 1 -n 3"
ret = subprocess.call(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(ret)

命令行参数

  • sys.argv
import sys
if __name__ == "__main__":
    args = sys.argv
    if len(args) == 1:
        print("exit: no arg")
        sys.exit(1)

    print("script: %s" % args[0])
    for i in xrange(1, len(args)):
        print("arg %s: %s" % (i, args[i]))
  • argparse
import argparse
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=u"命令行使用", )
    parser.add_argument("-n", help=u"字符串")
    parser.add_argument("-b", "--bool", help=u"布尔", action="store_true")
    args = parser.parse_args()
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    if args.n:
        print("n: %s" % args.n)
    if args.bool:
        print("bool: %s" % args.bool)

http

  • requests
#!/bin/env python2.7
# coding: utf8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import requests

# 发送get简单请求, http://baidu.com/s?w=qq&rn=1
req = requests.get("http://baidu.com/s", params={"wd": "qq", "rn": 1})
# 获取url, 状态码, cookies
print(req.url, req.status_code, req.cookies.items())
# 获取返回的html内容
print(req.content)

# 获取返回的json内容
req = requests.get("https://sposter.net/proxy/getnum")
print(req.json())

# 发起post请求, 查询IP归属
req = requests.post(url="https://tool.lu/ip/ajax.html", data={"ip": "27.38.56.29"})
data = req.json()
print("%s: %s" % (data["text"]["ip"], data["text"]["location"]))

# 自定义请求头部
headers = {
    "User-Agent": "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
    "Referer": "http://www.baidu.com"
}
proxies = {"http": "http://127.0.0.1:10000", "https": "http://127.0.0.1:10000"}
cookies = {"a": "b"}
req = requests.post(
    "https://tool.lu/ip/ajax.html",
    data={"ip": "27.38.56.29"},
    proxies=proxies,
    headers=headers,
    cookies=cookies
)
req_header = req.request.headers
print(req_header["cookie"], req_header["User-Agent"], req_header["Referer"])

# put, delete, 类似post
requests.put(url="", data={})
requests.delete(url="")

系统运维

  • psutil
import psutil
# cpu核数
print(u"cpu核数:%s" % psutil.cpu_count())

# cpu利用率
print(u"cpu使用率:%s%%" % psutil.cpu_percent())

# 内存信息
memory = psutil.virtual_memory()
memory_total = memory.total >> 20
memory_available = memory.available >> 20
memory_used = memory.used >> 20
memory_percent = memory.percent
print(u"内存:总%sM,可用%sM,已用%sM,使用率%s%%" % (memory_total, memory_available, memory_used, memory_percent))

# 磁盘空间
disk = psutil.disk_usage("/")
print(disk.total, disk.free, disk.used, disk.percent)
# 磁盘IO性能
io = psutil.disk_io_counters()
print(io)

# 网络
devs = psutil.net_if_addrs()
for dev in devs:
    for ip in devs[dev]:
        if ip.netmask:
            print("%s:%s" % (dev, ip.address))

# 进程
for proc in psutil.process_iter():
    p = proc.as_dict(attrs=['pid', 'name', 'username', 'cmdline'])
    print(p)

# 按进程名查找
def find_procs_by_name(name):
    ls = []
    for proc in psutil.process_iter():
        p = proc.as_dict(attrs=['name', 'pid', 'cmdline'])
        if p.info['name'] == name:
            ls.append(p)
    return ls


print(psutil.boot_time())
print(psutil.users())
  • platform
import platform

# 系统平台、版本
platform.system()
platform.version()
platform.platform()
platform.system()
  • IPy
import IPy

ip_net = "192.168.0.0/30"
ip_addr = "192.168.0.100"

# 输出子网内的所有IP
for ip in IPy.IP(ip_net):
    print(ip)

# 判断IP是否在网段内
print(ip_addr in IPy.IP(ip_net))

# 解析IP子网掩码长度
print(IPy.IP(ip_net).prefixlen())
#

数据库

  • MySQL-python
#!/bin/env python2
# coding: utf8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import MySQLdb


class MysqlClt():
    def __init__(self, host="127.0.0.1", port=3306, user="", passwd="", database=""):
        self.conn = MySQLdb.connect(
            host=host,
            port=port,
            user=user,
            passwd=passwd,
            db=database,
            charset="utf8",
        )

    def __del__(self):
       self.close()

    def close(self):
        try:
            self.conn.close()
        except:
            pass

    def query(self, sql, param=None):
        cur = self.conn.cursor()
        try:
            cur.execute(sql, param)
            result = cur.fetchall()
            return result
        except Exception as e:
            return "%s:%s" % (e[0], e[1])
        finally:
            cur.close()

    def execute(self, sql, param=None):
        cur = self.conn.cursor()
        try:
            rows = cur.execute(sql, param)
            self.conn.commit()
            return rows
        except Exception as e:
            self.conn.rollback()
            return "%s:%s" % (e[0], e[1])
        finally:
            cur.close()

if __name__ == "__main__":
    db = MysqlClt(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="root",
        database="test"
    )
    rows = db.query("show tables")
    print([row[0] for row in rows])
    rows = db.query("select id, name from name")
    print({row[0]: row[1] for row in rows})
    rows = db.execute("insert into name (name) values (%s)", "abc")
    print(rows)
  • redis
class RedisClt():
    def __init__(self, host="127.0.0.1", port=6379, auth="", db=0):
        self.conn = redis.StrictRedis(host=host, port=port, password=auth, db=db)

    def __getattr__(self, item):
        if hasattr(self.conn, item):
            return getattr(self.conn, item)

if __name__ == "__main__":
    db = RedisClt(host="127.0.0.1", port=6379, auth="12345678", db=0)

    info = db.info()
    print(info)

    result = db.set("name", "fcbox")
    print(result)

    value = db.get("name")
    print(value)
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值