老师,我的mocorunner窗口一直没有请求被重定向过来
不知道是哪里出错了,
我的代码如下:
mock_server1.py:
*********************************************************************
import os
import sys
basePath = os.getcwd()
print("**********",basePath)
sys.path.append(basePath)
from mitmproxy import http
from chapter7.util.handle_json import jsonsOperator
import json
''''''
class MockServer:
def request(self,flow):
requestData = flow.request
self.url = requestData.url
print("self.url------------>",self.url)
param = requestData.query
webform = requestData.urlencoded_form
requestData.host = "http://127.0.0.1"
requestData.port = 5000
def response(self,flow):
if "csdn" in self.url:
response = flow.response
headers = response.headers
urlList = self.url.split(".net")
baseUrl = urlList[0]
pathUrl = urlList[1]
if "?" in pathUrl:
pathUrl = pathUrl.split("?")[0]
print("pathUrl------>",pathUrl)
data = jsonsOperator.getValue(pathUrl)
print("type(data)------>",type(data))
print("data------>",data)
# flow.response.set_text(data)
response.set_text(json.dumps(data,ensure_ascii=False))
addons = [
MockServer()
]
*********************************************************
run_main_mitmproxy.py:
*******************************************************
import os
import sys
import json
#解决无法导入包的问题
basePath = os.getcwd()
sys.path.append(basePath)
from chapter7.util.handle_excel import handleExcel
from chapter7.base.base_request import baseRequest
from chapter7.util.handle_init import handleInit
from chapter7.util.handle_result import handleResult
from chapter7.util.handle_cookie import handleCookie
from chapter7.util.handle_header import handleHeader
from chapter7.util.handle_precondition import handlePrecondition
# print(basePath)
# print(__package__)
class RunMain:
def runCase(self):
rows = handleExcel.getMaxRows()
# print(rows)
for item in range(rows):
#print("now item is----------------->",item)
rowData = handleExcel.getRowValue(item+2)
#每条case执行前初始化cookie是None,初始化ifWriteCookie
preValue = None
cookieValue = None
ifWriteCookie = None
header = None
#注意:rowData是一个list,下标从0开始!!!
#print(type(rowData),rowData)
executed = rowData[int(handleInit.getValue("executed"))]
# print(type(executed),executed)
if executed == "yes":
caseId = rowData[int(handleInit.getValue("caseId"))]
url = rowData[int(handleInit.getValue("url"))]
method = rowData[int(handleInit.getValue("method"))]
expectWay = rowData[int(handleInit.getValue("expectWay"))]
expect = rowData[int(handleInit.getValue("expect"))]
headerMethod = rowData[int(handleInit.getValue("headerMethod"))]
data = rowData[int(handleInit.getValue("data"))]
print("====>",type(data),"||",data)
data = json.loads(data)
print("json.loads()后====>",type(data),"||",data)
'''precondition会改变本case data的传参值'''
precondition = rowData[int(handleInit.getValue("precondition"))]
if precondition:
preValue = handlePrecondition.getPreValue(precondition)
print("run_main preValue--->",preValue)
dependKey = rowData[int(handleInit.getValue("dependKey"))]
#更新本case请求参数
data[dependKey] = preValue
cookieMethod = rowData[int(handleInit.getValue("cookieMethod"))]
#Excel里只有post请求对应json参数,get请求对应params参数的时候headerMethod才可以是None或者空
if(headerMethod != None and headerMethod != ''):
header = handleHeader.getHeader(headerMethod,"/chapter7/config/header.json")
else:
#如果Excel里header是None或者空或者no,那么#headerMethod和header都是空
headerMethod = None
if cookieMethod == "yes":
cookieValue = handleCookie.getCookie("web","/chapter7/config/cookie.json")
print("获得了cookie值---》",cookieValue)
#如果这个接口是登录接口,对应cookieMethod是write,就赋值ifGetCookie=True
if cookieMethod == "write":
ifWriteCookie = {"cookie2":"web"}
#****cookie 为 yes,write或者no均会执行这句代码,写入cookie的任务放到baseRequest里。为了能取得res
res = baseRequest.run_main(method,url,data,headerMethod,header,cookieValue,ifWriteCookie)
# code = res.get("code")
# if(expectWay == "code"):
# if code == expect:
# handleExcel.writeToExcel(item+2,int(handleInit.getValue("result"))+1,"通过")
# handleExcel.writeToExcel(item+2,int(handleInit.getValue("res"))+1,json.dumps(res,ensure_ascii=False))
# else:
# print("expect == code result is--->",expect == code,expect,"|",code)
# handleExcel.writeToExcel(item+2,int(handleInit.getValue("result"))+1,"失败")
# handleExcel.writeToExcel(item+2,int(handleInit.getValue("res"))+1,json.dumps(res,ensure_ascii=False))
print("*****************************************")
if(__name__ == "__main__"):
runMain = RunMain()
runMain.runCase()
***********************************************************************
mocorunner窗口:
抓包发现请求确实被发送出去了但是没有重定向