一、token加盐处理
# import itsdangerous
#
# salt='sdf234^#$@G'
# t = itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=30)
# # res = t.dumps({'username':'yangfan','user_id':1})
# # token = res.decode()
# # print(token)
# s='eyJhbGciOiJIUzI1NiIsImlhdCI6MTU0MTgyMDA1NiwiZXhwIjoxNTQxODIwMDg2fQ.eyJ1c2VybmFtZSI6InlhbmdmYW4iLCJ1c2VyX2lkIjoxfQ.FUfs92HuVKrt61AKpMjv1Iye8QDP7XUGOfgcrSusMv8'
# res = t.loads(s)
# print(res)
import pymysql
coon = pymysql.connect()
cur= coon.cursor()
# sqls = ['sleect','uodate','delet','insert']
# for i in sqls:
# cur.execute(i)
二、sys.argv的用法
import flask
import sys
import time
server = flask.Flask(__name__)
@server.route('/')
def index():
return '<h1>success</h1>'
if len(sys.argv)>1:
port = sys.argv[1]
if port.isdigit():
server.run(port=port)
elif port=='--help':
print('这个python文件的作用是让你发财!')
elif port=='--time':
print(time.strftime('%Y-%m-%d %H:%M:%S'))
else:
print('端口号必须是整数!')
else:
print('运行错误!请在运行的时候指定端口号\n'
'请按照下面的方式运行python文件!\n'
'python mock_server.py 8989')
#sys.argv的作用是获取到运行python文件时,传入的参数
# python xxx.py --help
# 默认如果运行python文件的时候,不传参数,argv里面只有一个元素
#就是当前这个python文件的文件名
# python xxx.py
三、glob模块的用法和Exception以及finally的用法
import os,glob
# print(os.listdir())
# for f in os.listdir():
# if f.endswith('.py'):
# os.remove(f)
print(glob.glob('*')
# print(d['abc'])
# s='a'
# print(int(s))
# print(10/0)i
# import pymysql
# coon = pymysql.connect(host='118.24.3.40',user='jxz',
# password='123456',db='jxz')
# cur= coon.cursor()
# cur.execute('select from a;')
#
# for i in range(20):
# print(i)
# try:
# s = 10/0
# except ZeroDivisionError as e:
# # except ZeroDivisionError, e: 这个是python2里面的写法
# print('走这里')
# print(e)
def calc(a,b):
try:
res = a/b
# except ZeroDivisionError as e:
# res = '除数不能为零, %s'%e
# except TypeError as e:
# res = '类型错误,只能数字类型 %s'%e
except Exception as e:
print(e)
# return res
# res = calc('k',1) #TypeError
# # calc(10,0) # ZeroDivisionError
# res = calc(10,0) #TypeError
money = input('enter:')
try:
money = int(money)
except Exception as e:#产生异常了,走这边
print('输入金额错误!')
else:#没有出现异常的话就这里
print(money+1)
finally:
print('什么时候执行finally')
四、面向对象(定义类、self的用法、继承)
class People: #新式类
eye = 2
mouth = 1
shengao = 180
money=1000000
def __init__(self,name):
self.name = name
#构造函数,类在初始化做的一些操作
print('造了一个人,这个人是%s'%name)
def cry(self):
print('哭。。。')
def makeMoney(self):
print('self的内存地址',id(self))
print('%s 挣了20w'%self.name )
xiaojun = People('小军') #实例化
# print('小军的内存地址',id(xiaojun))
xiaojun.makeMoney()
# People.makeMoney(xiaojun)
# hailong = People('海龙')
# print('海龙的内存地址',id(hailong))
# hailong.makeMoney()
class Car:
def __init__(self,color,pl):
self.color = color
self.pl = pl
def add_fun(self,fun):
self.fun = fun
print('颜色是%s'%self.color)
print('排量是%s'%self.pl)
def help(self):
print('这个汽车的颜色【%s】'%self.color)
print('这个汽车的排量【%s】'%self.pl)
# print('这个汽车的功能【%s】'%self.fun)
BMW=Car('红色','3.5L')
# BMW.add_fun('水陆两栖')
BMW.help()
# add_fun(BMW,)
import pymysql
class MySQL:
def __init__(self,host,user,password,db,port=3306,charset='utf8'):
self.conn = pymysql.connect(host=host,user=user,password=password,db=db,port=port,charset=charset)
self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
#初始化的时候就连接数据库
def execute_many(self,sql):
self.cur.execute(sql)
return self.cur.fetchall() # [ {},{} ]
def execute_one(self,sql):
self.cur.execute(sql)
return self.cur.fetchone()
def __del__(self):
self.cur.close()
self.conn.close()
print('连接已经关闭')
db=MySQL('118.24.3.40','jxz','123456','jxz')
class People:
country = 'China' #类变量
def __init__(self,name,sex):
self.name = name #实例变量
self.sex = sex
def say(self):
print('name '+self.name)
print('sex'+self.sex)
print('country'+self.country)
@property #把一个函数变成一个变量,这个变量的值就是函数的返回值
def get_name(self):
return self.name
print(People.country)
xiaojun = People("xiaojun",'男')
xiaojun.say()
print(xiaojun.get_name)
class Phone:
def __del__(self):
print('哈哈哈哈')
def call(self,name):
print('为%s打call,为%s打电话!'%(name,name))
def __init__(self):
self.test = 'abc'
print('我是构造函数')
iphonx = Phone()
print(iphonx.test)
iphonx.call('小军')
class Lw:
money = 100000
house = '10套'
def driver(self):
print('开车')
def chouyan(self):
print('抽烟')
def hejiu(self):
print('喝酒')
def tangtou(self):
print('烫头')
class Xw(Lw):
def huaqian(self):
print('花钱。。。')
class Student():
def sql(self):
print('sql')
def linux(self):
print('linux')
class PyStudent(Student):
def python(self):
print('python')
class XnStudent(Student):
def xn(self):
print('xn')
def sql(self):
print('sql....')