s = 0
for i in xrange(10):
s += i
print(s)
s, i = 0, 1
while i <10:
s += i
i += 1
print(s)
与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"中文"
if __name__ == "__main__":
main()
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__)
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")
文件打开模式:
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)
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("文件日志")
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))
# 拆分字符串
string = "host1,host2,host3"
list_str = string.split(",")
print("%s 第一个元素为: %s" % (list_str, list_str[0]))
string = "host1,host2,host3"
new_string = string.replace("host", "server")
print(new_string)
new_string2 = string.replace("host", "server", 1)
print(new_string2)
# strip 去除两端指定字符,默认为空白符(空格,换行)
string = " fcbox\n"
print(string.strip())
# lstrip 去除左端指定字符,默认为空白符(空格,换行)
print(string.lstrip())
# rstrip 去除右端指定字符,默认为空白符(空格,换行)
print(string.rstrip())
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)
import time
randnum = int(time.time())
print(randnum)
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))
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)
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]))
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)
#!/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="")
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())
import platform
# 系统平台、版本
platform.system()
platform.version()
platform.platform()
platform.system()
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())
#
#!/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)
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)