Python 我将flask部署在公司服务器上托管iis。。我进行了配置,但在post请求中遇到了问题
我需要通过邮递员将照片发送到此flask api,以便在其上进行ocr,但在发送post请求时总是出现错误,在发送get请求时效果良好Python 我将flask部署在公司服务器上托管iis。。我进行了配置,但在post请求中遇到了问题,python,image-processing,iis,flask,Python,Image Processing,Iis,Flask,我需要通过邮递员将照片发送到此flask api,以便在其上进行ocr,但在发送post请求时总是出现错误,在发送get请求时效果良好 import pytesseract from PIL import Image pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract' from skimage import io import numpy as np from sk
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
from skimage import io
import numpy as np
from skimage.filters import threshold_otsu
from skimage.morphology import closing, square
import os
from flask import Flask, flash, request, redirect , url_for ,render_template
from werkzeug.utils import secure_filename
from cv2 import cv2
import re
UPLOAD_FOLDER = "C:\inetpub\wwwroot\ocr"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app.secret_key = b'_5#y5L"F4Q8z\n\xec]/'
@app.route('/bar',methods=['GET', 'POST'])
def bar():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return ('nothing')#redirect(request.url)
file = request.files.get('file')
#file = request.files.get['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return ('no file name')
elif file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
img = io.imread(filepath, as_gray = True)
#img = cv2.imread(filepath)
result = ocr(img)
#file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#filepath = (os.path.join(app.config['imgdir'], filename))
return (result)
#return ('okkkk')
return ('hello')
if __name__ == '__main__':
app.run(host='0.0.0.0',port=9010)
即使我通过邮递员发送了照片,如果“文件”不在,当我删除时,始终返回“nothing”
request.files:
flash('No file part')
return 'nothing'
我得到邮递员错误(500内部服务器错误)我认为问题是文件(照片)不是
是从邮递员那里收到的还是不接受照片?任何人都有解决办法。。谢谢
下面是web.config:
<configuration>
<system.webServer>
<handlers>
<add name="FlaskHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python\python.exe|C:\inetpub\wwwroot\ocr\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
<add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python\python.exe|C:\inetpub\wwwroot\ocr\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpErrors errorMode="Detailed" />
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="app.app" />
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\ocr" />
</appSettings>
</configuration>
我从邮递员那里发送文件时遇到了类似的问题,但我同时发送文件和JSON数据,但我开始在邮递员中使用
表单数据
,通过POST
和一些我需要的JSON额外数据将文件发送到服务器
然后在我的flask应用程序端点中,我这样阅读:
request.files[“文件名”]
此外,您应该使用来自werkzeug
的secure\u文件名
使文件安全,例如来自werkzeug的导入secure\u文件名
请注意,当您调用此代码时,请确保:
如果request.files中没有“file”:
闪存(“无文件部分”)
return('nothing')#重定向(request.url)
您已经在request
file
中命名了您的文件,如果没有,那么您在request
字典中无法通过此键找到它,因为它不存在。我必须提到,它在anaconda开发服务器上运行良好。谢谢。。您知道如何确定请求中的文件名是什么吗?因为我在PostMan中找不到它,因为语句“if request.files”给出true表示文件已收到,但当我写入file=request.files['file']时,给出的错误表示我已收到但无法访问。您可以通过写入print(request.files)来调试它
在您的代码中,这将打印文件的内容
,您将看到收到的文件的名称。谢谢。。你们的回答对我帮助很大,现在我的api在服务器上运行良好