The essence of the web framework (the most basic web framework implemented using socket, the web framework implemented using wsgiref)

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 contains all the data sent by the customer
    #start_response encapsulates the data to be returned to the user (response headers, status, etc.)
    print data
    start_response('200 OK',[("Content-Type","text/html")])
    #return content
    return  '<h1>Hello word </h1>'

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

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325059580&siteId=291194637