接口和协议组成 python mock 脚本

范旭斐 · 2016年04月23日 · 最后由 Leticia 回复于 2016年04月27日 · 2713 次阅读

rt,mock 的工具有很多,但是我觉得还是不如自己写脚本 mock 随意,方便。

#! usr/bin/python #coding=utf-8 
import BaseHTTPServer  
import urlparse  
import time
from SocketServer import ThreadingMixIn
import threading
import os

class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):  

    def do_POST(self):

        print 'post message'
        parsed_path = urlparse.urlparse(self.path) 
        paramstr = parsed_path.query
        path = parsed_path.path
        print paramstr
        print path
        for param in paramstr.split('&'):
            print param

        length = self.headers.getheader('content-length');
        nbytes = int(length)
        data = self.rfile.read(nbytes)
        cur_thread = threading.currentThread()
        print 'Thread:%s\tdata:%s' % (cur_thread.getName(), data)

        message_parts = [ 'just a test']  
        message = '\r\n'.join(message_parts)  
        self.send_response(200)  
        self.end_headers()  
        self.wfile.write(message)  

    def do_GET(self):

        print 'get message'
        parsed_path = urlparse.urlparse(self.path) 
        paramstr = parsed_path.query
        path = parsed_path.path
        print paramstr
        print path
        for param in paramstr.split('&'):
            print param

        if path == self.getShopLevelURL:
            buf = self.getShopLevelResponseBody
        else:
            buf = 'it works'
            self.send_header("Welcome", "Contect")

        self.protocal_version = 'HTTP/1.1'   
        self.send_response(200)  
        self.end_headers()  
        self.wfile.write(buf)

    '''获取店铺评级接口'''
    getShopLevelURL = '/abtest'
    getShopLevelResponseBody = '''
        {
            "status": 1,
            "info": "success",
            "data": {
                "input": {
                    "phone_number": "15168329206",
                    "cookie": null,
                    "id": "1234567",
                    "session_id": null,
                    "active_id": null
                },
                "abt_type": "b",
                "abt_name": "abt_001"
            }
        }'''

class ThreadingHttpServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
    pass

if __name__ == '__main__':
    '''server = BaseHTTPServer.HTTPServer(('0.0.0.0',18360), WebRequestHandler) '''
    server = ThreadingHttpServer(('0.0.0.0',18360), WebRequestHandler)
    ip, port = server.server_address
    '''Start a thread with the server -- that thread will then start one '''
    '''more thread for each request'''
    server_thread = threading.Thread(target=server.serve_forever)
    '''Exit the server thread when the main thread terminates'''
    server_thread.setDaemon(True)
    server_thread.start()

    print "Server loop running in thread:", server_thread.getName()
    while True:
       pass
共收到 4 条回复 时间 点赞

太直接了。。。先说明一下遇到什么痛点,为何市面上的 mock 都不合适,然后自己为了解决这个痛点采用什么方案,最后再贴代码。这样大伙比较容易理解。

我感觉不错,用什么工具不重要,思路正确就可以,这段代码活很清楚,自己用的惯就好。

—— 来自 TesterHome 官方 安卓客户端

我建议改用 flask 实现,代码量少的很

mark 先

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册