码迷,mamicode.com
首页 > 编程语言 > 详细

python3 使用http.server模块 搭建一个简易的http服务器

时间:2018-01-19 17:53:36      阅读:744      评论:0      收藏:0      [点我收藏+]

标签:this   for   elf   控制   服务器   response   code   his   ase   

 

from http.server import HTTPServer, BaseHTTPRequestHandler
import json

data = {result: this is a test}
host = (localhost, 8888)

class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header(Content-type, application/json)
        self.end_headers()
        self.wfile.write(json.dumps(data).encode())

if __name__ == __main__:
    server = HTTPServer(host, Resquest)
    print("Starting server, listen at: %s:%s" % host)
    server.serve_forever()

 

启动服务,在控制台看到:

技术分享图片

 

在浏览器输入http://localhost:8888/进行访问:

技术分享图片

 

此时查看控制台能看到log已经打印了请求:

技术分享图片

 

python3 使用http.server模块 搭建一个简易的http服务器

标签:this   for   elf   控制   服务器   response   code   his   ase   

原文地址:https://www.cnblogs.com/shenshangzz/p/8318143.html

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