实验5.1:多进程编程-多个函数并发执行
题目描述:利用multiprocessing多进程包完成以下2项编程任务。实验效果如图1-1所示。
(1)分别创建8个函数worker_1( )、worker_2( )、worker_3( )、worker_4( )、worker_5( )、worker_6( )、worker_7( )、worker_8( )。
(2)分别将这8个函数创建为8个进程,并实现并发执行。
提示:使用multiprocessing.Process( )函数创建进程,start( )函数启动进程。
import multiprocessing
import time
def worker_1(interval):
print("worker_1 start.")
time.sleep(interval)
print("worker_1 end.")
def worker_2(interval):
print("worker_2 start.")
time.sleep(interval)
print("worker_2 end.")
def worker_3(interval):
print("worker_3 start.")
time.sleep(interval)
print("worker_3 end.")
def worker_4(interval):
print("worker_4 start.")
time.sleep(interval)
print("worker_4 end.")
def worker_5(interval):
print("worker_5 start.")
time.sleep(interval)
print("worker_5 end.")
def worker_6(interval):
print("worker_6 start.")
time.sleep(interval)
print("worker_6 end.")
def worker_7(interval):
print("worker_7 start.")
time.sleep(interval)
print("worker_7 end.")
def worker_8(interval):
print("worker_8 start.")
time.sleep(interval)
print("worker_8 end.")
if __name__ == "__main__":
p1 = multiprocessing.Process(target=worker_1, args=(2,))
p2 = multiprocessing.Process(target=worker_2, args=(3,))
p3 = multiprocessing.Process(target=worker_3, args=(4,))
p4 = multiprocessing.Process(target=worker_4, args=(5,))
p5 = multiprocessing.Process(target=worker_5, args=(6,))
p6 = multiprocessing.Process(target=worker_6, args=(7,))
p7 = multiprocessing.Process(target=worker_7, args=(8,))
p8 = multiprocessing.Process(target=worker_8, args=(9,))
p1.start()
p2.start()
p3.start()
p4.start()
p5.start()
p6.start()
p7.start()
p8.start()
print("____________________START____________________")
print("The number of CPU is:" + str(multiprocessing.cpu_count()))
for p in multiprocessing.active_children():
print("child p.name:" + p.name + "\tp.id" + str(p.pid))
print("____________________END____________________")
实验5.2:多线程编程-定时自动关闭窗口
题目描述:利用tkinter图形界面库和threading多线程包完成以下2项编程任务。
(1)利用tkinter库设计如图2-1所示的程序界面。
(2)分别创建3个函数autoClose1( )、autoClose2( )和autoClose3( ),然后利用threading库将这3个函数创建为3个线程,并实现并发执行。
提示:使用threading.Thread( )函数创建线程,start( )函数启动线程。
import tkinter
import threading
import time
win = tkinter.Tk()
win.title("倒计时自动关闭窗口--mzx")
win.geometry("510x478")
t = tkinter.Text(win)
t.place(x = 10,y = 10,width=480,height=240)
t.insert(1.1,"mmmmcskccbdsjvsd")
Time1 = tkinter.Label(win,fg = "red",anchor="w")
Time1.place(x = 10,y = 250,width=170)
Time2 = tkinter.Label(win,fg = "orange",anchor="w")
Time2.place(x = 10,y = 275,width=170)
Time3 = tkinter.Label(win,fg = "blue",anchor="w")
Time3.place(x = 10,y = 300,width=170)
def autoClose1():
for i in range(10):
Time1["text"] = "距离窗口关闭还有{}秒".format(10-i)
time.sleep(1)
def autoClose2():
for i in range(20):
Time2["text"] = "距离窗口关闭还有{}秒".format(20-i)
time.sleep(1)
def autoClose3():
for i in range(25):
Time3["text"] = "距离窗口关闭还有{}秒".format(25-i)
time.sleep(1)
t1 = threading.Thread(target=autoClose1)
t1.start()
t2 = threading.Thread(target=autoClose2)
t2.start()
t3 = threading.Thread(target=autoClose3)
t3.start()
win.mainloop()
实验5.3:Socket网络编程-服务器和客户端通信
题目描述:利用tkinter图形界面库和socket套接字包完成以下3项编程任务。
(1)利用socket库创建1个服务器端实时监听程序,绑定主机IP地址(默认127.0.0.1)和端口(8123)。用于实时监听客户端状态,接收客户端消息,并即时发送消息给对应客户端。程序如图3-1所示。
(2)利用tkinter库和socket库分别创建2个客户端收发消息程序,连接服务器端主机,实现与服务器端之间消息发送与消息接收的功能。2个客户端程序界面如图3-2所示。
(3)实现服务器和客户端的连接及单对多的并发通信功能。
提示:使用socket.socket( )创建服务器端,bind( )绑定监听端口,listen( )实现监听,accept( )接受客户端连接和地址,recv( )接收消息,send( )发送消息;使用socket.socket( )创建客户端1和客户端2,connect( )连接服务器,recv( )接收消息,send( )发送消息。
from multiprocessing.connection import Client
import socket
from time import ctime
bufsize = 1024
host = '127.0.0.1'
port = 8123
address = (host,port)
server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sock.bind(address)
server_sock.listen(1)
while True:
print('start waiting......')
clientsock ,addr = server_sock.accept()
print('received from:',addr)
while True:
data = str(clientsock.recv(bufsize),encoding="utf-8")
print('received --> %s\n%s' %(ctime(),data))
data = input("send -->")
clientsock.send(bytes(data,encoding="utf-8"))
clientsock.close()
server_sock.close()
###客户
from socket import *
from time import ctime
bufsize = 1024
host = '127.0.0.1'
port = 8123
address = (host,port)
client_sock = socket(AF_INET,SOCK_STREAM)
client_sock.connect(address)
while True:
data = input("发送 --> ")
if not data:
break
else:
client_sock.send(bytes(data,encoding="utf-8"))
data = str(Client_sock.recv(bufsize),encoding="uft-8")
print('收到-->%s\n%s'%ctime(),data)
client_sock.close()
实验5.4:综合Web编程-Flask框架
题目描述:利用Web开发框架包flask完成4项编程任务。
(1)创建门户信息展示部分。分别建立4个路由渲染呈现4个网页。
路由@app.route(’/’)呈现门户信息主页index.html;如图4-1所示。
路由@app.route(’/test1’)呈现信息显示子页show_test1.html;
路由@app.route(’/test2’)呈现信息显示子页show_test2.html;
路由@app.route(’/test3’)呈现信息显示子页show_test3.htm。
其中show_test1.html、show_test2.html、show_test3.html三个信息显示子页是通过模板生成网页show.html生成模板框架并模板显示信息的。
在index.html主页中分别通过超级链接进入各自子页。
(2)创建前端用户平台部分。分别建立5个路由渲染呈现4个网页。
路由@app.route(’/user_login’)呈现用户登录主页user_login.html;如图4-2所示。
路由@app.route(’/user_platform’)呈现用户平台主页user_platform.html;
路由@app.route(’/user/’)呈现参数传递子页query_user.html;如图4-3所示。
路由@app.route(’/query_user’)呈现参数传递子页query_user.html;如图4-4所示。
路由@app.route(’/query_url’)呈现方向路由子页query_url.html。如图4-5所示。
其中@app.route(’/user/’)和@app.route(’/query_user’)共用呈现query_user.html;
(3)创建后台管理平台部分。分别建立2个路由渲染呈现2个网页。
路由@app.route(’/admin_login’)呈现管理登录主页admin_login.html;如图4-6所示。
路由@app.route(’/admin_platform’)呈现管理平台主页admin_platform.html。
(4)创建其他信息提示部分。分别建立1个路由和两个错误句柄渲染呈现3个网页。
路由@app.route(’/info’)呈现其他信息子页info.html;如图4-7所示。
路由@app.errorhandler(404)呈现错误信息子页404.html;如图4-8所示。
路由@app.errorhandler(500)呈现错误信息子页500.html。如图4-9所示。
提示:
(1)Web系统架构一般分为四个组成部分,分别是门户信息展示、前端用户平台、后台管理平台和其他信息提示。
(2)本实验需要建立1个flask_web根目录,其中包含1个flask_web.py文件和1个templates子目录。
flask_web.py文件代码包含12个路由和2个错误句柄,用于启动Web服务和运行对应程序功能。
templates子目录包含13个网页。
主目录结构与文件数量如图4-10所示。
(3)使用Flask( )创建app.route( )实现路由,render_template( )呈现网页,request.args.get( )传递参数,flash( )获取数据,url_for( )反向路由,abort( )错误中止,app.run( )运行服务。
# Python程序
from flask import Flask, request, url_for, render_template, flash, abort
app = Flask(__name__)
app.secret_key = "123"
# 门户信息展示
@app.route('/') # 路由
def index():
title1 = "index"
header1 = "Welcome to index page"
content1 = "This is the index page!"
content2 = ["hello python", "hello mza", "hello kalof"]
# 信息主页index.html
return render_template('index.html', title=title1, header=header1, content=content1, contents=content2)
@app.route('/test1') # 正向路由1
def test1():
title1 = "show"
header1 = "Welcome to show_test1 page"
content1 = "This is the test1 page!"
# 网页show.html,子页show_test1.html
return render_template('show_test1.html', title=title1, header=header1, content=content1)
@app.route('/test2') # 正向路由2
def test2():
title1 = "show"
header1 = "Welcome to show_test2 page"
content1 = "This is the test2 page!"
# 网页show.html,子页show_test2.html
return render_template('show_test2.html', title=title1, header=header1, content=content1)
@app.route('/test3') # 正向路由1
def test3():
title1 = "show"
header1 = "Welcome to show_test3 page"
content1 = "This is the test3 page!"
# 网页show.html,子页show_test3.html
return render_template('show_test3.html', title=title1, header=header1, content=content1)
# 前端用户平台
@app.route('/user_login') # 二级路由
def user_login():
title1 = "user_login"
header1 = "Welcome to user_login page"
content1 = "Hello mzx, this is the user login page!"
# 登录主页user_login.html
return render_template('user_login.html', title=title1, header=header1, content=content1)
@app.route('/user_platform')
def user_platform():
title1 = "user_platform"
header1 = "Welcome to user_platform page"
content1 = "Hello mzx, this is the user platform page!"
# 用户平台主页user_platform.html
return render_template('user_platform.html', title=title1, header=header1, content=content1)
@app.route('/user/<uid>', methods=['GET', 'POST'])
def user_id(uid):
title1 = "query_user"
header1 = "Welcome to query_user page"
content1 = "Hello mzx, this is the query user page!"
content2 = "query user:" + uid
# 渲染呈现参数传递子页query_user.html
return render_template('query_user.html', title=title1, header=header1, content1=content1, content2=content2)
@app.route('/query_user', methods=['GET', 'POST'])
def query_user_uid():
try:
uid = request.args.get("uid")
uname = request.args.get("uname")
if int(uid): # 判断必须是整数
title1 = "query_user"
header1 = "Welcome to query_user page"
content1 = "Hello mzx, this is the query user page!"
if uname:
content2 = "query user: id=" + uid + ", " + "name=" + uname
else:
content2 = "query user: id=" + uid
# 传递子页query_user.html
return render_template('query_user.html', title=title1, header=header1, content1=content1, content2=content2)
else:
abort(500) # 500错误中止码
except:
abort(500) # 500错误中止码
@app.route('/query_url') # 反向路由
def query_url():
title1 = "query_url"
header1 = "Welcome to query_url page"
content1 = "Hello mzx, this is the query url page!"
content2 = "query url:" + url_for('user_login')
# 反向路由子页query_url.html
return render_template('query_url.html', title=title1, header=header1, content1=content1, content2=content2)
# 后台管理平台
@app.route('/admin_login', methods=['GET', 'POST']) # 二级路由
def admin_login():
title1 = "admin_login"
header1 = "Welcome to admin_login page"
content1 = "Hello mzx, this is the admim login page!"
# 管理登录主页admin_login.html
return render_template('admin_login.html', title=title1, header=header1, content=content1)
@app.route('/admin_platform')
def admin_platform():
title1 = "admin_platform"
header1 = "Welcome to admin_platform page"
content1 = "Hello mzx, this is the admin platform page!"
# 管理平台主页admin_platform.html
return render_template('admin_platform.html', title=title1, header=header1, content=content1)
# 其他信息提示
@app.route('/info')
def info():
title1 = "info"
header1 = "Welcome to info page"
flash("Hello python3 and mzx!") # 代替content, 需要关联app.secret_key="123"配合使用
# 信息子页info.html
return render_template('info.html', title=title1, header=header1)
@app.errorhandler(404) # 错误句柄,提示404错误信息
def not_found1(e):
title1 = "error:404"
header1 = "Welcome to error:404 page"
content1 = "This page went to Mars!"
# 错误信息子页404.html
return render_template('404.html', title=title1, header=header1, content=content1)
@app.errorhandler(500) # 错误句柄,提示500错误信息
def not_found2(e):
title1 = "error:500"
header1 = "Welcome to error:500 page"
content1 = "This page has been aborted!"
# 渲染呈现错误信息子页500.html
return render_template('500.html', title=title1, header=header1, content=content1)
# 主程序入口
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5050)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
404
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>500</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
500
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>admin_login</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
admin_login
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>admin_platform</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
admin_platform
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>index</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
index
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>info</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
info
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>query_user</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
query_user
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>query_ult</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
query_ult
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>show_test1</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
show_test1
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>show_test2</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
show_test2
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>show_test3</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
show_test3
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>user_login</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
user_login
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>user_platfrom</title>
<script>
alert("我真帅");
document.write("我真帅");
console.log(xxx);
</script>
<style>
body {
height: 2000px;
width: 1000px;
margin: auto;
}
h2 {
font-size: 80px;
color: red;
font-family: 'Courier New', Courier, monospace;
}
p {
font-size: 30px;
color: blue;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<div>
<h2>
user_platfrom
</h2>
<p>
macbscbdshcscdslkchcnsajcsdbcsd
</p>
</div>
</body>
</html>