zoukankan      html  css  js  c++  java
  • 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"}
    
    
  • 相关阅读:
    JDBC
    Servlet的优缺点
    css样式,媒体查询,垂直居中,js对象
    浏览器渲染优化
    css秘密花园
    express 4
    redux
    koa
    webpack
    react学习总结
  • 原文地址:https://www.cnblogs.com/wangymd/p/11011616.html
Copyright © 2011-2022 走看看