• python2.7实现本地启一个http服务作为数据转发接收器或模拟接口响应数据源


    from http.server import HTTPServer, BaseHTTPRequestHandler
    import json
     
     
    class Resquest(BaseHTTPRequestHandler):
        def handler(self):
            print("data:", self.rfile.readline().decode())
            self.wfile.write(self.rfile.readline())
     
        def do_GET(self):
            print(self.requestline)
            if self.path != '/hello':
                self.send_error(404, "Page not Found!")
                return
     
            data = {
                'result_code': '1',
                'result_desc': 'Success',
                'timestamp': '',
                'data': {'message_id': '25d55ad283aa400af464c76d713c07ad'}
            }
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(data).encode())
     
     
        def do_POST(self):
            print(self.headers)
            print(self.command)
            req_datas = self.rfile.read(int(self.headers['content-length']))    #获取请求内容
            print(req_datas.decode())
            if self.path != '/walle':
                self.send_error(404, "Page not Found!")
                return
            data = {
                'result_code': '2',
                'result_desc': 'Success',
                'timestamp': '',
                'data': {'message_id': '25d55ad283aa400af464c76d713c07ad'}
            }
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(data).encode('utf-8'))
    
    
    if __name__ == '__main__':
        host = ('', 9002)
        server = HTTPServer(host, Resquest)
        print("Starting server, listen at: %s:%s" % host)
        server.serve_forever()
  • 相关阅读:
    匿名方法
    C# 正则表达式
    c# 预处理命令
    反射
    特性(attribute)
    c# 交换两个变量
    构造函数
    泛型
    Event事件
    委托
  • 原文地址:https://www.cnblogs.com/relustarry/p/13633770.html
Copyright © 2020-2023  润新知