如何在运行时与flask应用程序进行交互?

时间:2016-11-24 16:23:54

标签: python flask

如何使用flask shell / cli或任何其他选项以不间断的方式与我的运行烧瓶应用程序进行交互,因此pdb不好...

例如,假设我有以下应用程序:

from flask import Flask, request


app = Flask(__name__)
received_data = []


@app.route('/', methods=['POST'])
def slash():
    data = request.data
    if data not in received_data:
        received_data.append(data)
    return request.data

if __name__ == '__main__':
    app.run(debug=True)

我发送了几个请求:

➤➤ curl -X POST -H 'Content-Type: application/json' http://127.0.0.1:5000 -d {'a':'1'}
> {a:1}

➤➤ curl -X POST -H 'Content-Type: application/json' http://127.0.0.1:5000 -d {'b':'2'}
> {b:2}

...

现在我想启动一个控制台,并在应用程序仍在运行时以不间断的方式检查全局变量received_data的值。另外,我想以类似于rails控制台的方式与应用程序进行交互:app.get,app.post,ex:app.post('/', {"this" => "that"})

1 个答案:

答案 0 :(得分:0)

您需要做的是添加一个端点

 public IEnumerable<System.Drawing.Image> ExtractImagesFromPDF(string sourcePdf)
    {
        // NOTE:  This will only get the first image it finds per page.
        var pdf = new PdfReader(sourcePdf);
        var raf = new RandomAccessFileOrArray(sourcePdf);

        try
        {
            for (int pageNum = 1; pageNum <= pdf.NumberOfPages; pageNum++)
            {
                PdfDictionary pg = pdf.GetPageN(pageNum);

                // recursively search pages, forms and groups for images.
                PdfObject obj = ExtractImagesFromPDF_FindImageInPDFDictionary(pg);
                if (obj != null)
                {
                    int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(CultureInfo.InvariantCulture));
                    PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                    PdfStream pdfStrem = (PdfStream)pdfObj;
                    PdfImageObject pdfImage = new PdfImageObject((PRStream)pdfStrem);
                    System.Drawing.Image img = pdfImage.GetDrawingImage();
                    yield return img;
                }
            }
        }
        finally
        {
            pdf.Close();
            raf.Close();
        }
    }