Django基础入门
Posted 胸不平$怎平天下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django基础入门相关的知识,希望对你有一定的参考价值。
HTTP协议规格:
\'GET / HTTP/1.1 <-- 请求行
Host: 127.0.0.1:8001 <-- 请求头部
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: _ga=GA1.1.522660093.1520652325; csrftoken=gYuOvvynQQLeetX7Bxj6naEyQbOgH9pLBphdWvMTmK5nos3b8QZRhTA5y4DnQG3M
\' <-- 没有请求数据
###################1.简单的sokect web框架:#####################
import sockesk = socket.socket()
sk.bind(("127.0.0.1",8080))
sk.listen()
while 1:
conn,addr =sk.accept()
date = conn.recv(1024)
print(date)
#这是一个socket链接web的一个协议
conn.send((b"http/1.1 200 ok \\r\\ncontent-type:text/html: charset=utf-8\\r\\n\\r\\n"))
conn.send(b"helle sb") #1.给web返回一个send消息,2.send消息时候也可用html标签来包装内容:<h1>hello s10!</h1>
conn.close()
sk.close()
sokect给浏览器返回结果:
################ 2.请求不同的web返回不同的内容###################
import socket
sk = socket.socket()
sk.bind(("127.0.0.1",8001))
sk.listen()
while 1:
conn,addr = sk.accept()
data =conn.recv(1024)
data_str = str(data,encoding="utf-8")
l1 = data_str.split("\\r\\n")
l2 = l1[0].split()
url =l2[1]
conn.send(b"http/1.1 200 ok \\r\\ncontent-type:text/html; charset=utf-8\\r\\n\\r\\n")
if url ==\'/yimi/\':
response = b\'<h>hello we</h>\'
elif url ==\'/welcome/\':
response=b\'<h1>Welcome to login </h1>\'
else:
response =b\'<h1>404! not foud</h1>\'
conn.send(response)
conn.close()
sk.close()
server端给web返回结果:
################ 3.函数版本请求的web返回不同的内容#############
import socket
sk = socket.socket()
sk.bind((\'127.0.0.1\',80))
sk.listen()
#定义一个处理hellowd的函数
def hellowd(url):
ret = \'welconme {}\'.format(url)
return bytes(ret,encoding="utf-8")
#定义第二个web处理函数
def xiaoxue(url):
ret =\'hei is {}\'.format(url)
return bytes(ret,encoding="utf-8")
while 1:
conn,add = sk.accept()
date = conn.recv(1024)
date_str = str(date,encoding="utf-8")
l1 =date_str.split("\\r\\n")
l2 = l1[0].split()
url = l2[1]
conn.send(b\'http/1.1 200 ok\\r\\nconten-type:text/html; charset=utf-8\\r\\n\\r\\n\')
if url ==\'/hellowd/\':
response =hellowd(url)
elif url ==\'/xiaoxue/\':
response =xiaoxue(url)
else:
response =b\'<h1>404! not file</h1>\'
conn.send(response)
conn.close()
#########通过web返回一个html的页面##############
import socket
s = socket.socket()
s.bind(("127.0.0.1",8001))
s.listen()
def yimi(url):
with open("yimi.html","rb") as f:
ret =f.read()
return ret
def login(url):
with open("login.html","rb") as f:
ret = f.read()
return ret
def f404(url):
ret = \'你访问的{}找不到\'.format(url)
return bytes(ret,encoding="utf-8")
url_func =[
("/yimi/",yimi),
("/login/",login),
]
while 1:
conn,add = s.accept()
date = conn.recv(1024)
date_str =str(date,encoding="utf-8")
l1 = date_str.split(\'\\r\\n\')
l2 = l1[0].split()
url = l2[1]
conn.send(b\'http/1.1 200 ok \\r\\ncontent-type:text/html; charset=ut-8\\r\\n\\r\\n\')
for i in url_func:
if i[0] ==url:
func = i[1]
break
else:
func =f404
response =func(url)
conn.send(response)
conn.close()
server端给web返回html登录页面结果:
#########返回一个html动态的页面##############
以上是关于Django基础入门的主要内容,如果未能解决你的问题,请参考以下文章
Django基础之model操作数据库model基础入门推荐