请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,我的请求没有重定向成功,辛苦老师帮忙看下

老师,我的mocorunner窗口一直没有请求被重定向过来

https://img1.sycdn.imooc.com//szimg/60e852e6093cf05d15370301.jpg

不知道是哪里出错了,

我的代码如下:

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()

***********************************************************************

https://img1.sycdn.imooc.com//szimg/60e8546e0924344c19400405.jpg


mocorunner窗口:

https://img1.sycdn.imooc.com//szimg/60e854a209204b1317550098.jpg

https://img1.sycdn.imooc.com//szimg/60e8548a09f21d9a15090326.jpg


抓包发现请求确实被发送出去了但是没有重定向

https://img1.sycdn.imooc.com//szimg/60e855c009afc68f24371253.jpg

正在回答 回答被采纳积分+3

1回答

Mushishi 2021-07-10 21:02:41

额。你这个都啥跟啥啊。为啥mitmproxy+fiddler  这俩一起用?有没有被模拟 你直接设置一个自定义返回结果哦。

https://img1.sycdn.imooc.com//szimg/images/unknow-80.png而且你这个监听的是8084端口 你发送请求走的啥端口?额。你走偏了

0 回复 有任何疑惑可以回复我~
  • 提问者 cloudonthesun #1
    谢谢老师指点,我把fiddler关闭,把代理打开,端口填写为mitmproxy端口,问题解决了
    回复 有任何疑惑可以回复我~ 2021-07-11 10:33:15
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信