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

Python简单http服务实现

时间:2019-06-12 19:48:03      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:import   cal   creat   get   rom   __name__   tar   hand   _for   

1、代码实现

# -*- coding: utf-8 -*-
"""
Created on Tue Jun 11 18:12:01 2019

@author: wangymd
"""

from http.server import HTTPServer, BaseHTTPRequestHandler
import json

data = {‘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"}

Python简单http服务实现

标签:import   cal   creat   get   rom   __name__   tar   hand   _for   

原文地址:https://www.cnblogs.com/wangymd/p/11011616.html

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