Python is simple http service implementation

1, code implementation

# -*- 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, the test

The browser calls:

http://localhost:8888/

Return the following:

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

Guess you like

Origin www.cnblogs.com/wangymd/p/11011616.html