1.c/s架构
c/s:客户端 —> 服务端(MySQL是客户端和服务端在同一台机器上)
优点:
缺点:
2.b/s架构
优点:
缺点:
3.基于socket写一个web应用
main.py
import socket
def server_run():
soc = socket.socket()
soc.bind(('127.0.0.1', 8008))
soc.listen(5)
while True:
conn, addr = soc.accept()
recv_data = conn.recv(1024)
print(recv_data)
# 1 直接在send里写,发送给客户端
# conn.send(b'HTTP/1.1 200 OKrnrn<h1>hello web</h1><img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg"></img>')
#2 打开一个html文件,发送给客户端
# with open('index.html','r',encoding='utf-8') as f:
# data=f.read()
# conn.send(('HTTP/1.1 200 OKrnrn%s'%data).encode('utf-8'))
# 3 动态网页,字符串替换
import time
now=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(now)
with open('index.html','r',encoding='utf-8') as f:
data=f.read()
data=data.replace('@@@',now)
conn.send(('HTTP/1.1 200 OKrnrn%s'%data).encode('utf-8'))
conn.close()
if __name__ == '__main__':
server_run()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>@@@</h2>
<img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg" alt="">
</body>
</html>
4.手写web框架
main.py
import socket
import pymysql
def index(request):
return '<img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30/sign=5e3814acf9edab64607f4592965fc4a6/14ce36d3d539b600c0c465d0eb50352ac65cb74b.jpg"></img>'
def login(request):
with open('login.html','r',encoding='utf-8') as f :
data=f.read()
return data
def time(request):
import datetime
now=datetime.datetime.now().strftime('%Y-%m-%d %X')
with open('time.html','r',encoding='utf-8') as f :
data=f.read()
data=data.replace('@@time@@',now)
return data
def user_list(request):
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='lqz')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id,name,password from user")
user_list = cursor.fetchall()
cursor.close()
conn.close()
tr_list=[]
for row in user_list:
tr='<tr><td>%s</td><td>%s</td><td>%s</td></tr>'%(row['id'],row['name'],row['password'])
tr_list.append(tr)
with open('user_list.html','r',encoding='utf-8') as f:
data=f.read()
data=data.replace('@@body@@',''.join(tr_list))
return data
def user_list_new(request):
# 创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='lqz')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("select id,name,password from user")
user_list = cursor.fetchall()
cursor.close()
conn.close()
with open('user_list_new.html','r',encoding='utf-8') as f:
data=f.read()
from jinja2 import Template
template=Template(data)
response=template.render(user_list=user_list)
# response=template.render({'user_list':user_list})
return response
urls = [
('/index', index),
('/login', login),
('/time', time),
('/user_list', user_list),
('/user_list_new', user_list_new),
]
def run():
soc = socket.socket()
soc.bind(('127.0.0.1', 8006))
soc.listen(5)
while True:
conn, port = soc.accept()
data = conn.recv(1024)
# data=data.decode('utf-8')
print(data)
data = str(data, encoding='utf-8')
request_list = data.split('rnrn')
head_list = request_list[0].split('rn')
method, url, htt = head_list[0].split(' ')
# conn.send(b'hello web')
conn.send(b'HTTP/1.1 200 OK rnrn')
print(url)
func_name = None
for u in urls:
if url == u[0]:
func_name = u[1]
break
if func_name:
response = func_name(data)
else:
response = '404 not found'
conn.send(response.encode('utf-8'))
conn.close()
if __name__ == '__main__':
run()
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="">
<p>用户名:<input type="text"></p>
<p>密码:<input type="password"></p>
</form>
</body>
</html>
time.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
@@time@@
</body>
</html>
user_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
@@body@@
</tbody>
</table>
</body>
</html>
user_list_new
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
</tr>
</thead>
<tbody>
{% for user in user_list%}
<tr>
<td>{{user.id}}</td>
<td>{{user.name}}</td>
<td>{{user.password}}</td>
</tr>
{%endfor%}
</tbody>
</table>
</body>
</html>
二.HTTP协议
1.什么是HTTP协议
2.HTTP协议的作用
3.HTTP版本及区别
0.9:功能捡漏,只支持GET方法,只能发送HTML格式字符串。
1.1:默认持久连接、请求管道化、增加缓存处理、增加Host字段、支持断点传输分块传输等。
3.0:弃用TCP,改用基于UDP协议的QUIC协议代替TCP协议。
4.HTTP协议的特点
5.HTTP请求协议
一个HTTP请求报文(也称请求消息)由请求行(request line)、请求头(header)、空行和请求数据(也称请求体)4个部分组成
请求行
请求头1
请求头2
……
请求空行
请求体
请求行:由请求字段、URL字段和HTTP协议版本字段三部分组成,用空格分隔
请求行格式:Method空格Request-URL空格HTTP-Version rn
rn:表示换行
常用的请求头:
6.HTTP响应协议
HTTP响应也由三个部分组成,分别是:状态行、响应头、空行、响应正文
状态行
响应头1
响应头2
……
响应空行
响应体
状态行格式:HTTP-Version空格Status-Code空格Reason-Phrase
HTTP-Version表示服务器HTTP协议的版本,例如为HTTP/1.1
常见的HTTP状态码:
原文地址:https://blog.csdn.net/qq_65852978/article/details/134652077
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_16245.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。