#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,os
import hashlib
import pickle
import time
import socketserver
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import modules
from lib.modules import *
class Myserver(socketserver.BaseRequestHandler):
def md5(self,pwd):
'''
对密码进行加密
:param pwd: 密码
:return:
'''
hash=hashlib.md5(bytes('xx7',encoding='utf-8'))
hash.update(bytes(pwd,encoding='utf-8'))
return hash.hexdigest()
def login(self,usrname,pwd,x):
'''
登陆
:param usrname: 用户名
:param pwd: 密码
:return:是否登陆成功
'''
conn=self.request
if x=='1':
path_name_pwd=os.path.join(settings.ADMIN,usrname)
else:
mulu=os.path.join(settings.ALL_USERS,usrname)
path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
s=pickle.load(open(path_name_pwd,'rb'))
if usrname in s:
if s[usrname]==self.md5(pwd): #和加密后的密码进行比较
return True
else:
return False
else:
return False
def regist(self,usrname,pwd,x):
'''
注册
:param usrname: 用户名
:param pwd: 密码
:return:是否注册成功
'''
conn=self.request
if x=='1':
mulu=os.path.join(settings.ADMIN,usrname)
else:
mulu=os.path.join(settings.ALL_USERS,usrname)
if os.path.exists(mulu):
return False
else:
os.mkdir(mulu)
s={}
s[usrname]=self.md5(pwd)
path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
pickle.dump(s,open(path_name_pwd,'wb'))
path_name_base=os.path.join(mulu,usrname+'name_base')
pickle.dump([15000,{},0,0,0],open(path_name_base,'wb'))
path_name_liushui=os.path.join(mulu,usrname+'name_liushui')
os.mkdir(path_name_liushui)
return True
def user_identity_authentication(self,usrname,pwd,ret,x):
'''
判断注册和登陆,并展示用户的详细目录信息,支持cd和ls命令
:return:
'''
conn=self.request
if ret=='1':
r=self.login(usrname,pwd,x)
if r:
conn.sendall(bytes('y',encoding='utf-8'))
else:
conn.sendall(bytes('n',encoding='utf-8'))
elif ret=='2':
# print(usrname,pwd)
if x=='1':
r=self.regist(usrname,pwd,x)
else: #用户注册
s=[0,1]
pickle.dump(s,open(settings.A,'wb'))
while True:
ret=pickle.load(open(settings.A,'rb'))
if ret[0]==0:
time.sleep(30)
continue
elif ret[0]==1 or ret[0]==2:
break #默认值已更改,银行管理员已操作
if ret[0]==1: #如果管理员同意
r=self.regist(usrname,pwd,x)
else:
r=0
s=[0,0]
pickle.dump(s,open(settings.A,'wb'))
if r:
conn.sendall(bytes('y',encoding='utf-8'))
else:
conn.sendall(bytes('n',encoding='utf-8'))
def interactive(self,usrname): #进行交互
conn=self.request
while True:
c=conn.recv(1024) #接收用户交互选项
r=str(c,encoding='utf-8')
mulu=os.path.join(settings.ALL_USERS,usrname)
path_name_base=os.path.join(mulu,usrname+'name_base')
s=pickle.load(open(path_name_base,'rb'))
#打印账户信息
obj=modules.Atm(s[0],s[1],s[2],s[3],s[4],usrname) #Atm对象
a=obj.account_info() #接收账户信息
conn.sendall(bytes(a,encoding='utf-8'))
b=obj.ret_account_info()
if r== '4':
buy_obj=modules.buy(b[2],0,{})
amount=buy_obj.main()
elif r=='q':
break
else:
s=conn.recv(1024)
amount=str(s,encoding='utf-8')
obj.transaction(r,amount)
pass
def handle(self):
conn=self.request
x=conn.recv(1024)
x=str(x,encoding='utf-8')
conn.sendall(bytes('收到用户类别',encoding='utf-8'))
while True:
if x=='1' or x=='2':
b=conn.recv(1024)
ret=str(b,encoding='utf-8')
conn.sendall(bytes('b ok',encoding='utf-8'))
c=conn.recv(1024)
r=str(c,encoding='utf-8')
usrname,pwd=r.split(',')
print(usrname,pwd)
self.user_identity_authentication(usrname,pwd,ret,x) #登陆或注册验证
if x=='2':#普通用户身份验证成功后
self.interactive(usrname)
pass
break
elif x=='q':
break
if __name__=='__main__':
sever=socketserver.ThreadingTCPServer(('127.0.0.1',9999),Myserver)
sever.serve_forever()
3.4 用户端
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
本程序作为用户或银行管理员的入口,其中c=1代表银行管理员,c=2代表普通用户
'''
import pickle
import sys
import time
import os
import socket
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import *
from lib.modules import *
def login(usrname,pwd):
'''
登陆
:param usrname:用户名
:param pwd:密码
:return:是否登陆成功
'''
obj.sendall(bytes(usrname+','+pwd,encoding='utf-8'))
ret=obj.recv(1024)
r=str(ret,encoding='utf-8')
if r=='y':
return 1
else:
return 0
def regist(usrname,pwd,x):
'''
注册
:param usrname:用户名
:param pwd:密码
:return:是否注册成功
'''
obj.sendall(bytes(usrname+','+pwd,encoding='utf-8'))
ret=obj.recv(1024)
r=str(ret,encoding='utf-8')
if r=='y':
return 1
else:
return 0
def user_identity_authentication(usrname,pwd,x):
'''
选择登陆或注册,展示用户的详细目录信息,支持cd和ls命令
:return:
'''
a=input('请选择1.登陆 2.注册')
obj.sendall(bytes(a,encoding='utf-8'))
obj.recv(1024)
if a=='1':
ret=login(usrname,pwd)
if ret:
print('登陆成功')
return 1
else:
print('用户名或密码错误')
return 0
elif a=='2':
ret=regist(usrname,pwd,x)
if ret:
print('注册成功')
return 1
else:
print('用户名已存在或银行管理员拒绝')
return 0
def main(x):
usrname=input('请输入用户名')
pwd=input('请输入密码')
if user_identity_authentication(usrname,pwd,x): #如果验证身份成功
if x=='1': #处理用户注册信息
while True:
s=pickle.load(open(settings.A,'rb'))
if s[1]==0:
time.sleep(30)
continue
elif s[1]==1:
while True:
a=input('用户请求注册,输入1同意,2拒绝')
if a=='1':
s=[1,0]
pickle.dump(s,open(settings.A,'wb'))
break
elif a=='2':
s=[2,0]
pickle.dump(s,open(settings.A,'wb'))
break
else:
print('输入有误')
break
else: #普通用户登陆后
interactive() #进行交互
def interactive():
while True:
a=input('请选择 1.还款 2.取现 3.转账 4.消费 5.存钱 q退出')
obj.sendall(bytes(a,encoding='utf-8'))
r=obj.recv(1024) #接收账户信息
ret=str(r,encoding='utf-8')
print(ret)
if a !='4'and a !='q':
b=input('请输入金额')
obj.sendall(bytes(b,encoding='utf-8'))
elif a=='q':
break
obj=socket.socket() #创建客户端socket对象
obj.connect(('127.0.0.1',9999))
while True:
x=input('请选择1.银行管理员 2.用户 q、退出')
obj.sendall(bytes(x,encoding='utf-8'))
obj.recv(1024) #确认收到用户类别
if x=='1' or x=='2':
main(x)
break
elif x=='q':
break
else:
print('输入有误请重新输入')
obj.close()
3.5定时任务
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys
import json,pickle
import time
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
def main():
card_list = os.listdir(settings.ALL_USERS)
for card in card_list:
basic_info = pickle.load(open(os.path.join(settings.ALL_USERS, card, card+'name_base')))
struct_time = time.localtime()
# 循环账单列表,为每月的欠款计息。并写入到当月账单中
for item in basic_info['debt']:
interest = item['total_debt'] * 0.0005
if basic_info[4] >= interest:
basic_info[4] -= interest
else:
temp = interest - basic_info[4]
basic_info[4]=0
basic_info[0] -= temp
pickle.dump(
basic_info,
open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
)
# 如果当前等于10号(9号之前)
# 当前余额为负值,则将值添加到账单列表中,开始计息,同时,本月可用额度恢复。
date = time.strftime("%Y-%m-%d")
if struct_time.tm_mday == 11 and basic_info[2]>0:
dic = {'date': date,
"total_debt": basic_info[2],
"balance_debt": basic_info[2],
}
basic_info[1].append(dic)
# 恢复可用额度
basic_info[0] = 15000
pickle.dump(
basic_info,
open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
)
def run():
main()
以上就是用python开发网上商城信用卡系统,需要的同学可以参考下。
本文原创发布php中文网,转载请注明出处,感谢您的尊重!
相关文章
相关视频
网友评论
文明上网理性发言,请遵守 新闻评论服务协议我要评论
立即提交
专题推荐独孤九贱-php全栈开发教程
全栈 100W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
玉女心经-web前端开发教程
入门 50W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
天龙八部-实战开发教程
实战 80W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习