【问题标题】:catch PIL exceptions in Tornado在 Tornado 中捕获 PIL 异常
【发布时间】:2012-08-14 18:44:15
【问题描述】:

捕捉错误不会部分起作用!

我要做的是检测用户上传的是真实图片还是伪造图片(例如重命名为.jpeg的文本文件)

try:
    image = Image.open(StringIO.StringIO(buf=avat))
    type = image.format
    (x, y) = image.size
    print x, y
    if x < y:
        orientation = "portrait"
    else:
        orientation = "paysage" 
    pref = str(time.time())
    nomf = pref.replace(".", "") 
    nomfich = nomf+"."+type
    self.fs = GridFS(self.db)
    avatar_id = self.fs.put(avat, content_type=avctype, filename=nomfich)
except IOError, TypeError:
    self.redirect("/erreur-im")

这是http://localhost:8000/erreur-im中显示的足球

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\tornado-2.3.post1-py2.7.egg\tornado\web.py", line 1023, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "G:\Mon projet\sog-emouk\handlers.py", line 101, in get
    avctype = self.db.users.find_one()["avctype"]
TypeError: 'NoneType' object has no attribute '__getitem__'

在这里,我想做的是将用户重定向到一个页面,告诉他们用户必须使用图像图片。

当我将 self.redirect 更改为 self.write("please upload....") 时,结果很奇怪:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\tornado-2.3.post1-py2.7.egg\tornado\web.py", line 1023, in _execute
    getattr(self, self.request.method.lower())(*args, **kwargs)
  File "G:\Mon projet\sog-emouk\handlers.py", line 153, in post
    user={"pseudo":pseudo, "orientation":orientation, "avctype":avctype, "password":password, "email":email, "tel":tel, "commune":commune, "coord":coord, "statut":statut, "telf":telf, "avatar":avatar_id, "acheteur":[], "vendeur":[]}
UnboundLocalError: local variable 'orientation' referenced before assignment

所以,我猜self.redirect 在这里有些奇怪,因为self.write 在捕获异常后执行代码(实际上由于文本文件,image.size 不知道)。`

【问题讨论】:

    标签: python python-imaging-library tornado


    【解决方案1】:

    此回溯表明您正在重定向到在两个地方再次抛出未捕获异常的代码。 看到这个:avctype = self.db.users.find_one()["avctype"]

    user={"pseudo":pseudo, "orientation":orientation, "avctype":avctype, "password":password, "email":email, "tel":tel, "commune":commune, "coord":coord, "statut":statut, "telf":telf, "avatar":avatar_id, "acheteur":[], "vendeur":[]}
    

    查看两个回溯:

    Traceback (most recent call last):
      File "C:\Python27\lib\site-packages\tornado-2.3.post1-py2.7.egg\tornado\web.py", line 1023, in _execute
        getattr(self, self.request.method.lower())(*args, **kwargs)
      File "G:\Mon projet\sog-emouk\handlers.py", line 101, in get
        avctype = self.db.users.find_one()["avctype"]
    TypeError: 'NoneType' object has no attribute '__getitem__'
    

    Traceback (most recent call last):
      File "C:\Python27\lib\site-packages\tornado-2.3.post1-py2.7.egg\tornado\web.py", line 1023, in _execute
        getattr(self, self.request.method.lower())(*args, **kwargs)
      File "G:\Mon projet\sog-emouk\handlers.py", line 153, in post
        user={"pseudo":pseudo, "orientation":orientation, "avctype":avctype, "password":password, "email":email, "tel":tel, "commune":commune, "coord":coord, "statut":statut, "telf":telf, "avatar":avatar_id, "acheteur":[], "vendeur":[]}
    UnboundLocalError: local variable 'orientation' referenced before assignment
    

    所以我认为您在初始化应用程序和请求处理程序的应用程序中存在错误。

    另外你还有一个look at如何检查文件是否真的是jpeg。事实上,您可以检查其他媒体文件格式 avi、rm 等检查前四个字符。

    【讨论】:

    • 啊!现在我明白了,因为之前声明了avctype,所以阅读这里有错误!再次感谢您,并感谢您的链接;)
    猜你喜欢
    • 1970-01-01
    • 2010-10-03
    • 2012-07-10
    • 1970-01-01
    • 2011-05-29
    • 2011-07-02
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多