1:框架本质—>2:解决中文乱码—>3:根据不同路径返回不同页面—>4:函数版—>5:函数进阶版—>6:返回具体页面—>7:让网页动起来
1:框架本质:
import socket
server_sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1',9999))
server_sk.listen(128)
while True:
new_sk,addr = server_sk.accept()
content = new_sk.recv(1024)
content = content.decode('utf-8')
print('接收到了数据:')
print(content)
new_sk.send('HTTP/1.1 200 ok\r\n\r\n'.encode('utf-8'))
# new_sk.send(b'HTTP/1.1 200 ok\r\n\r\n')
#请求首行 。b把str转为字节(二进制),转不了中文
new_sk.send('ok'.encode('utf-8'))
new_sk.close()
#问题:中文乱码
详细解释连接:https://blog.csdn.net/aixiangnan/article/details/89482398
2:解决中文乱码
import socket
server_sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(("127.0.0.1",9999))
server_sk.listen(128)
while True:
#等待传入连接
client_sk,addr = server_sk.accept()
content = client_sk.recv(1024) #设置套接字接收最多缓冲大小字节,设置阻塞,防止发送的数据过大造成数据丢失,默认二进制
content = content.decode("utf-8") # 从二进制转为utf-8编码格式
print("接收到数据了:")
print(content)
msg1 = 'HTTP/1.1 200 ok\r\n'.encode('utf-8') #设置响应首行
msg2 = 'Content-Type:text/html;charset=utf-8\r\n'.encode('utf-8')
#设置响应头 (内容类型及字符集)
msg3 = '\r\n'.encode('utf-8') #设置空行
# msg4 = '你好浏览器'.encode('utf-8') #设置响应体
msg4 = str(input('请回答:')).encode('utf-8')
cl