Python簡單http服務實現


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"}
 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM