码迷,mamicode.com
首页 > Web开发 > 详细

web框架的本质(使用socket实现的最基础的web框架、使用wsgiref实现的web框架)

时间:2018-04-28 10:43:32      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:for   address   bin   main   list   .so   nbsp   hello   end   

import socket

def handle_request(client):
    data = client.recv(1024)
    client.send("HTTP/1.1 200 OK\r\n\r\n")
    client.send("<h1>Hello word</h1>")

def main():
    sockobj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sockobj.bind((‘localhost‘,8888))
    sockobj.listen(5)

    while True:
        con1,address = sockobj.accept()
        handle_request(con1)
        con1.close()

if __name__ == "__main__":
    main()

  

#coding=utf-8
from wsgiref.simple_server import  make_server

def Runserver(data,start_response):
    #data里面包含的是客户发来的所有数据
    #start_response 封装了要返回给用户的数据(响应头、状态等)
    print data
    start_response(‘200 OK‘,[("Content-Type","text/html")])
    #返回的内容
    return  ‘<h1>Hello word </h1>‘

if __name__ == "__main__":
    httpobj = make_server(‘‘,8888, Runserver)
    print ‘port HTTP on port 8888‘
    httpobj.serve_forever()

  

 

web框架的本质(使用socket实现的最基础的web框架、使用wsgiref实现的web框架)

标签:for   address   bin   main   list   .so   nbsp   hello   end   

原文地址:https://www.cnblogs.com/qiangayz/p/8965646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!