【问题标题】:POST request results in "internal server error", most of the times but not all (!?)POST 请求导致“内部服务器错误”,大多数情况下但不是全部(!?)
【发布时间】:2015-07-17 23:34:36
【问题描述】:

编辑:

tl;博士:

如何使用 python 请求将图像发布到可以使用 openCV 操作的烧瓶应用程序?

问题的长版本和我尝试过的东西:

我正在尝试将图像从一个 Python Flask 应用程序发布到另一个。我面临的问题是它有时似乎有效,但通常不会。

接收数据的应用:

@app.route("/post", methods=['GET', 'POST'])
def post():
    print "in post"
    req = request.files['file']
    #    req = open('pug.jpg', 'r+b')
    arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
    img = cv2.imdecode(arr,-1) # 'load it as it is'
    print img
    cv2.imwrite('result.png',img)
    return "Hello"

发送数据的应用:

def hello():
    url = 'http://IP_ADDRESS/post'
    #This image will not work
    image = open('PATH/TO/IMAGE/pug.jpg', 'r+b')
    #But with this image it will work!?
    #image = open('PATH/TO/IMAGE/res_tile.png', 'r+b')
    files = {'file': image}
    r = requests.post(url, files=files)
    image.close()
    return 'Hello World!'

我尝试过的:

  • 首先,我认为该特定图像有问题 但我已经尝试了一堆。
  • 我已删除 POST 并将其全部放入 在一个模块中。然后就可以了。

所以,我做 POST 的方式一定有问题;但它适用于其他图像。真奇怪。

任何帮助将不胜感激!

编辑: 我现在尝试了几种其他方法,包括接收和发送 POST 请求,但仍然没有运气。我发现了更多有效的图像和更多无效的图像。令人费解且非常恼人。更多代码见下文。

接收数据的应用:

def post():
    print "in post"
    photo = request.files['file']
    in_memory_file = io.BytesIO()
    photo.save(in_memory_file)
    data = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8)
    color_image_flag = 1
    img = cv2.imdecode(data, color_image_flag)
    print img
    cv2.imwrite('result.png',img)
    return "Hello"

发送数据的应用:

def hello():
    url = 'http://IPADDRESS/post'
    image = open('../PATH/TO/IMAGE/pug2.png', 'r+b')
    files = {'file': ('hej.png', image, 'image/png')}
    r = requests.post(url, files=files, stream=True)
    image.close()
    print r.content
    return 'Hello World!'

还是没有运气。

编辑:

现在我还尝试了 requests_toolbelt 中的“Multipartencoder”,但我仍然面临同样的问题。在某些图像上它有效。在其他人没有。有没有人知道我做错了什么?

发送请求的应用:

def hello():
        m = MultipartEncoder(
            fields={'field0': 'value', 'field1': 'value',
                    'field2': ('filename', open('../PATH/TO/IMG', 'rb'), 'image/png')}
        )
        print m.fields['field0']
        r = requests.post('http://192.168.0.3/post', data=m,
                  headers={'Content-Type': m.content_type})

接收请求的应用程序与以前相同,只是进行了更改,因此我访问了文件的正确字段(使用“req = request.files['field2']”)

欢迎任何提示/点击/想法!

【问题讨论】:

  • 实际错误是什么?如果您不确定,请在调试模式下运行您的应用程序。
  • 我不知道实际的错误。我正在使用调试模式,但由于我只从一个应用程序发布到另一个应用程序,因此我什么也没得到。 response.content 只会产生“内部服务器错误”。我尝试使用调试模式来解决这个问题,因此将 2 个应用程序合并为 1 个,从而消除了 POST ......但是它可以工作......所以我真的不知道。如果您在发布到烧瓶应用程序时对如何使用调试模式有任何提示,我将不胜感激。
  • 在调试模式下,您会在控制台中看到堆栈跟踪(或日志,具体取决于您运行应用程序的方式)。
  • 我的调试模式已开启。但是当这种情况发生时,我的控制台什么也没显示。我的代码中的最后 3 行是:1:“if name == 'main':”。 2:“app.debug = True”。 3:“manager.run()”
  • 我的帖子只是得到一个“内部服务器错误”响应,我的控制台是静默的。

标签: python flask http-post python-requests


【解决方案1】:

它“解决了”。或者更确切地说,我找到了一种解决方法。

两个应用程序(接收和发送图像)都在我的机器上本地运行。将我的接收应用程序推送到 AWS 并将我的发送应用程序保留在我的机器接缝上以解决问题(至少从我在初始测试中看到的情况来看)。我想同时在本地进行以更轻松地开发,但现在我需要让一个应用程序在非本地运行。

如果你们中的任何人知道为什么这现在有效......我全神贯注。

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多