---
title: "tornado搞个静态文件访问"
author: "splayx"
date: "2014-04-05T15:43:41Z"
tags: []
categories: ["开源软件"]
slug: "tornado"
draft: false
lastmod: "2014-04-05T15:43:41Z"
id: 2041370
url: "https://splayx.iteye.com/blog/2041370"
---

这个有啥用？比如在linux下生产一些图片、文本，可以用本地的浏览器打开。

```
#!/usr/bin/env python
#! coding: utf-8

import tornado.httpserver
import tornado.ioloop
import tornado.web

class Hello(tornado.web.RequestHandler):
    def get(self):
        self.write("hello world")

class MyFile(tornado.web.StaticFileHandler):
    def set_extra_headers(self, path):
        self.set_header("Cache-control", "no-cache")

app = tornado.web.Application([
    (r"/", Hello),
    (r"/myfile/(.*)", MyFile, {"path":"/tmp/"})
])

if __name__ == '__main__':
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(9999)
    tornado.ioloop.IOLoop.instance().start()
```