带Flask的iOS App

时间:2018-07-06 02:45:38

标签: python flask

我正在尝试将iOS应用捕获的图像上传到Flask上-我收到HTTP 500错误,我不了解其起源。

这是我的代码:

from flask import Flask
from flask_restful import reqparse, Api, Resource
from flask import request

app = Flask(__name__)
api = Api(app)

@app.route('/')
def projects():
    file = None
    if request.method == "POST":
        file = request.files['file']

    if file:
         img = Image.open(file)
         print("Image successfully loaded.")

    return render_template("home.html")

app.run(debug=True)

对于iOS:

func uploadImage(image: UIImage) -> Void{
        //Convert the image to a data blob
        guard let png = UIImagePNGRepresentation(image) else{
            print("error")
            return
        }

        //Set up a network request
        let request = NSMutableURLRequest()
        request.httpMethod = "POST"
        request.url = NSURL(string: "http://127.0.0.1:5000/") as! URL as! URL
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("\(png.count)", forHTTPHeaderField: "Content-Length")
        request.httpBody = png
        // Figure out what the request is making and the encoding type...

        //Execute the network request
        let upload = URLSession.shared.uploadTask(with: request as URLRequest, from: png) { (data: Data?, response: URLResponse?, error: Error?) -> Void in
            //What you want to do when the upload succeeds or fails
        }

        upload.resume()

    }

override func viewDidLoad() {
    super.viewDidLoad()

    let img = UIImage(named: "messi.jpg")
    uploadImage(image: img!)

}

有什么想法吗?

任何帮助将不胜感激。谢谢!

编辑:我得到的服务器上的错误是“ builtins.UnboundLocalError UnboundLocalError:分配前引用了本地变量'文件'”,但是如果我修复,则会得到“ jinja2.exceptions.TemplateRuntimeError:扩展了多次。”

我在Xcode上收到的消息是“ 715863 + 0900 swiftServer [6240:329748] [] nw_socket_output_finished shutdown(8,SHUT_WR)[57:套接字未连接]”

1 个答案:

答案 0 :(得分:0)

您需要在烧瓶侧查看错误消息。

p4 copy表示“内部服务器错误” ,这表示您的flask应用目前存在错误。也许您可以在服务器的控制台上找到详细的错误消息。