outputprocessor.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:BPRC 作者: bradwood 项目源码 文件源码
def writeOutput(self, *, writeformat, writefile, req):
        """Writes the output to the writefile in the format specified"""
        vlog("Generating output of step: " + str(self.id) +" " + self.step.name + ". Format=" + writeformat)

        if writeformat == 'json':
            formatted_json = json.dumps(self.step.response.body,indent=4, sort_keys=True)
            #colorise the json
            if sys.stdout.isatty():
                from pygments import highlight, lexers, formatters
                colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter())
                print(colorful_json, file=writefile)
            else:
                print(formatted_json, file=writefile)

        else: # format is raw-(all|response)
            printstepcolophon(self.step,id=self.id, file=writefile)


            if bprc.cli.args.nocolor:
                colourful = False
            else:
                colourful = sys.stdout.isatty()

            if writeformat == 'raw-all': ## need to write the http resquest first.
                print("-- Request --", file=writefile)
                printhttprequest(self.step, id=self.id, file=writefile, colourful=colourful)
                printheaders(self.step, id=self.id,file=writefile, http_part='request',  colourful=colourful)
                logging.debug("PRINTING REQUEST HEADERS")
                if self.step.request.body:
                    logging.debug("Req.body==" +req.body)
                    try: # if this doesn't decode then it's probably not JSON, just print it
                         # without a decode
                        self.step.request.body=json.loads(req.body)
                    except JSONDecodeError as e:
                        self.step.request.body=req.body
                    printbody(self.step, id=self.id, file=writefile, http_part='request',  colourful=colourful)
                print("-- Response --", file=writefile)

            # now write the response
            printhttpresponse(self.step, id=self.id, file=writefile, colourful=colourful)
            printheaders(self.step, id=self.id, file=writefile, http_part='response', colourful=colourful)

            if self.step.response.body:
                printbody(self.step, id=self.id, file=writefile, http_part='response',  colourful=colourful)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号