在Tornad中捕获PIL异常

2024-09-29 21:43:08 发布

您现在位置:Python中文网/ 问答频道 /正文

捕捉错误不一定奏效!在

我要做的是检测用户上传的是真实图片还是伪造图片(例如,一个重命名为.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中显示的taceball

^{pr2}$

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

当我把self.redirect改成{}时,结果很奇怪:

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在捕捉到异常之后会超出代码(实际上图像大小由于文本文件)`


Tags: 用户imageselftimetype图片redirect文本文件
1条回答
网友
1楼 · 发布于 2024-09-29 21:43:08

此回溯表明您正在重定向到在这两个位置再次引发未捕获异常的代码。 看这个: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":[]}

查看两个回溯:

^{pr2}$

以及

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等检查前四个字符。在

相关问题 更多 >

    热门问题