![](/img/trans.png)
[英]Cannot display the image in web browser provided by Python via Apache Server
[英]How to display image on web browser using Tornado Python server?
該錯誤表明縮進位於“ 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()
try
期待一個except
try:
with open(path, 'rb') as f:
data = f.read()
self.write(data)
self.finish()
except IOError:
print "Failed!!"
為了使其顯示為圖像,您需要設置內容標頭以反映它是圖像mime類型...
與get函數匹配的except塊在哪里? 您至少應在函數中添加以下內容。
except IOError as xcpt:
# IO error handling
raise xcpt # if you want to propagate the exception
如果您有一個單獨的圖像文件夾,並想通過url將其作為靜態內容提供,例如
http://yourwebsite.com/images/yourimage.jpg
然后可以使用tornado.web.StaticFileHandler
:
handlers = [
(r"/images/(.*)", tornado.web.StaticFileHandler, {'path': "./images"}),
(r"/", WebHandler)
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.