错误表示缩进位于" if _name"部分..我不确定为什么会表明这一点。
class GetFileHandler(tornado.web.RequestHandler):
def get(self):
fileid = self.get_argument('fileid', "")
cur.execute("""SELECT filepath FROM files_table WHERE file_id = %s""", (fileid, ))
m = cur.fetchall()
y = m[0]
x = y[0]
path = x + "/" + fileid + ".jpg"
try:
with open(path, 'rb') as f:
data = f.read()
self.write(data)
self.finish()
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[(r"/getit", GetFileHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
答案 0 :(得分:3)
try
需要except
try:
with open(path, 'rb') as f:
data = f.read()
self.write(data)
self.finish()
except IOError:
print "Failed!!"
为了让它显示为图像,您需要设置内容标题以反映它是图像的mime类型...
答案 1 :(得分:0)
匹配get函数的except块在哪里?您应该至少在函数中添加类似以下内容的内容。
except IOError as xcpt:
# IO error handling
raise xcpt # if you want to propagate the exception
答案 2 :(得分:0)
如果你有一个单独的图像文件夹,并希望将它们作为带有网址的静态内容提供,例如
http://yourwebsite.com/images/yourimage.jpg
然后您可以使用tornado.web.StaticFileHandler
:
handlers = [
(r"/images/(.*)", tornado.web.StaticFileHandler, {'path': "./images"}),
(r"/", WebHandler)
]