flask 图像接口测试

方法一:requests

import requests, time

class PicApi():
    def requests_post(self, path):
        url = self.url + '/get_result'
        files = {'image':open(path,'rb'),
                 'filename':path[:path.index('.')]}
        feed_back = str(requests.post(url,files=files).content)
        if '400' not in feed_back:
            print('【Info LOG】{} 图片上传成功!'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())))
            return True
        else:
            print('【Info LOG】{} 图片上传失败,返回信息:{}!'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()),feed_back))
            return False

    def request_download(self, path, opt):
        import requests
        if opt=='X2':
            r = requests.get(self.url+'/static/output/X2/X2_'+ path +'.png')
            with open('X2.png', 'wb') as f:
                f.write(r.content)
        else:
            r = requests.get(self.url+'/static/output/X4/X4_'+ path +'.png')
            with open('X4.png', 'wb') as f:
                f.write(r.content)
            
    def main(self, url, path, opt='X2'):
        self.url = url
        flag = self.requests_post(path)
        if flag:
            self.request_download(path, opt)
            print('【Info LOG】{} 图片下载成功!'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())))

PA = PicApi()
PA.main('http://7a337874388e.ngrok.io','111.png')

Thanks for Lin YuCheng.

方法二:curl

!curl -X POST -F image=@111.png http://7a337874388e.ngrok.io/get_result

在利用正则化从HTML中进行爬取

<body>
    <div align="center">
        <h1>Super Resolution Image</h1>

        
            <div class="row" align="center">
                <div class="column">
                    <h1>Output Image X2</h1>
                    <img src="static/output/X2/X2_111.png.png" height=512px width=512px />

                    <form action="/static/output/X2/X2_111.png.png" method="get" enctype='multipart/form-data'>
                        <input class="input" type="submit" name="download" value="Download">
                    </form>

                </div>


                <div class="column">
                    <h1>Output Image X4</h1>
                    <img src="static/output/X4/X4_111.png.png" height=512px width=512px />

                    <form action="/static/output/X4/X4_111.png.png" method="get" enctype='multipart/form-data'>
                        <input class="input" type="submit" name="download" value="Download">
                    </form>
                </div>

                <form action="/" method="get" enctype='multipart/form-data'>
                        <input class="input" type="submit" name="Continue" value="Continue">
                </form>

            </div>
        
    </div>
</body>
原文地址:https://www.cnblogs.com/zgqcn/p/14406466.html