使用Python 写一个最简单的WebServer

import  socket

def handle_request(client):
    buf = client.recv(1024)
    client.send("HTTP/1.1 200 OK\r\n\r\n")
    client.send('<h30>Hello World</h10>')

def main():
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.bind(('192.168.68.128',8006))
    sock.listen(1)
    while True:
         connection,address = sock.accept()
         handle_request(connection)
         connection.close()
if __name__ == '__main__':
    main()

 

以上是服务端代码,如果我们在客户端使用浏览器访问,返回helloworld到这里我们实现了一个最简单的webserver的服务端

posted @ 2016-05-12 20:30  FreeMan1  阅读(4535)  评论(0编辑  收藏  举报