通过代理下载url时的Flask gevent

时间:2016-03-01 17:49:24

标签: python flask gevent

我有一个简单的烧瓶应用程序,我正在通过gevent服务器运行。

app = Flask(__name__)

def console(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    while True:
        data = p.stdout.read(512)
        yield data
        if not data:
            break
        if isinstance(p.returncode, int):
            if p.returncode > 0:
                # return code was non zero, an error?
                print 'error:', p.returncode
            break

@app.route('/mp3', methods=['POST'])
def generate_large_mp3():
    video_url = "url.com"
    title = 'hello'

    mp3 = console('command')
    return Response(stream_with_context(mp3),  mimetype="audio/mpeg3",
                       headers={"Content-Disposition": 'attachment;filename="%s.mp3"' % filename})
if __name__ == '__main__':
    http_server = WSGIServer(('', 5000), app)
    http_server.serve_forever()

当我的控制台功能运行时,如果它通过代理运行以下载url而不是服务器的ip,我怎么能这样做呢?

1 个答案:

答案 0 :(得分:0)

回答了,

只需更新Popen中的env变量

即可
env = dict(os.environ)
env['http_proxy'] = proxies[random.randrange(0, len(proxies))]
env['https_proxy'] = proxies[random.randrange(0, len(proxies))]