【发布时间】:2012-04-02 10:56:33
【问题描述】:
我正在使用 tornado、python 编写一个小型 Web 应用程序,下面是我的代码。我在 python 中有一个带有 2 个文本字段的 html 表单,现在我想将输入表单作为文本字段并存储在 redis 中。
我的问题 -
- 如何从我的 python 脚本连接到 redis?
- 如何将传入的用户输入存储到 redis 中?
示例代码将不胜感激。
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('<html><body><form action="/" method="post">'
'<p>Please enter the Key Value pair for redis.</p>'
'<input type="text" **name="key"** value="type key here">'
'<input type="text" **name="value"** value="type value here">'
'<input type="submit" value="Submit Key Value">'
'</form></body></html>')
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
【问题讨论】:
-
连接redis的python redis模块、方法的redis文档和关于POST处理的tornado文档怎么样?