Python简单http服务实现

我怕爱的太早我们不能终老 提交于 2020-02-05 07:57:27

1、代码实现

# -*- coding: utf-8 -*-"""Created on Tue Jun 11 18:12:01 2019@author: wangymd"""from http.server import HTTPServer, BaseHTTPRequestHandlerimport jsondata = {'result': 'this is a http server 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 http server, listen at: %s:%s" % host)    server.serve_forever()2、测试

浏览器调用:

http://localhost:8888/

返回如下内容:

{"result": "this is a http server test"}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!