一,主程序
import socket
"""
1.有固定的ip和port
2.24小时不间断提供服务
3.能够承受高并发
请求首行
b'GET / HTTP/1.1
请求头(K:V的形式)
Host: 127.0.0.1:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: csrftoken=3vPenhmlRQb8Tvl4okwYM0OZpDCl3P7rbxvfpRDOHJy1zUApw89ugxM6OZSxhIBM
请求体(只不过现在这里我们没有发数据,所以这里是空的)
'
"""
server = socket.socket() # 默认是TCP协议
server.bind(('127.0.0.1',8080))
server.listen(5)
while True:
conn, addr = server.accept()
data = conn.recv(1024)
conn.send(b'HTTP/1.1 200 OK
')
res = data.decode('utf-8')
current_path = res.split('
')[0].split(' ')[1]
# print(current_path)
if current_path == '/index':
# conn.send(b'index')
# with open('templates/111.html','rb') as f:
with open(r'E:周末四期day19代码day19复习bootstrap页面搭建 1 图书管理系统前端页面.html','rb') as f:
conn.send(f.read())
###在这个地方我加载这个图书馆管理页面的时候,只可以加载出其他的文字信息,但是加载不出这个CSS样式
###是因为这个文件的引入有问题,前端浏览器访问不到这个CSS样式的文件资源
##TODO:这个问题等后期学习完Django之后再来解决
elif current_path == '/login':
with open(r'E:周末四期day19代码day19 emplatesget_user.html',mode='rb') as w:
conn.send(w.read())
# conn.send(b'login')
elif current_path == '/show_time':
with open(r'E:周末四期day19代码day19 emplatesshow_time.html',mode='rb') as t:
conn.send(t.read())
else:
conn.send(b'404')
conn.close()
二,HTML文件
文件1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="../bootstrap-3.3.7/css/bootstrap.min.css">
<script src="../bootstrap-3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../font-awesome-4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">同济大学图书馆</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">四平校区 <span class="sr-only">(current)</span></a></li>
<li><a href="#">嘉定校区</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">更多操作 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="搜索">
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">孙志平</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">更多操作 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">修改密码</a></li>
<li><a href="#">修改头像</a></li>
<li><a href="#">后台管理</a></li>