我的Tornado应用程序通过http正文请求接受POST数据
在我的处理程序中,我能够得到请求
def post(self):
data = self.request.body
Run Code Online (Sandbox Code Playgroud)
我得到的数据来自str(字典)
有没有办法以Python字典的形式接收这些数据?
我不想eval在服务器端使用将此字符串转换为Python字典.
我读到了关于龙卷风的信息:
另一方面,如果您已经拥有一个WSGI应用程序并希望在快速的tornado.httpserver.HTTPServer上运行它,请使用tornado.wsgi.WSGIContainer进行包装.但你需要小心.由于您的原始应用程序没有为异步服务器做好准备,并且会进行大量的IO /计算,因此它会在生成响应时阻止其他请求(进一步的请求将被接受并缓冲,但排队等待以后处理).
而Guincorn说:
'用于UNIX的Python WSGI HTTP服务器.这是一个从Ruby的Unicorn项目移植的前叉工作者模型.
epoll或kqueue完成工作(没有主人/工人流程)?requests.get在处理程序的get/post函数中),这将阻止所有请求处理或仅阻止当前正在处理的请求?我正在尝试通过websockets建立一个公共Twitter流的小例子.这是我的websocket.py,它正在运行.
我想知道的是:我如何从类WSHandler的"外部"与websocket进行交互(即,不仅仅是在收到来自websocket.js的消息时回答)?假设我想在同一个脚本中运行一些其他函数来发布"你好!" 每隔五秒钟将其发送到websocket(浏览器),而无需客户端的任何交互.我怎么能这样做?
因此,我认为这是一个基本的初学者问题,关于如何处理下面的类.任何方向的任何指针将不胜感激!
import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
# websocket
class FaviconHandler(tornado.web.RequestHandler):
def get(self):
self.redirect('/static/favicon.ico')
class WebHandler(tornado.web.RequestHandler):
def get(self):
self.render("websockets.html")
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'new connection'
self.write_message("Hi, client: connection is made ...")
def on_message(self, message):
print 'message received: \"%s\"' % message
self.write_message("Echo: \"" + message + "\"")
if (message == "green"):
self.write_message("green!")
def on_close(self):
print 'connection closed'
handlers = [
(r"/favicon.ico", FaviconHandler),
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
(r'/', WebHandler),
(r'/ws', WSHandler),
]
settings = …Run Code Online (Sandbox Code Playgroud) 如何重定向另一个页面并从表中传递url中的参数?我在tornato模板中创建了这样的东西
<table data-role="table" id="my-table" data-mode="reflow">
<thead>
<tr>
<th>Username</th>
<th>Nation</th>
<th>Rank</th>
<th></th>
</tr>
</thead>
<tbody>
{% for result in players %}
<tr>
<td>{{result['username']}}</td>
<td>{{result['nation']}}</td>
<td>{{result['rank']}}</td>
<td><input type="button" name="theButton" value="Detail"
></td>
</tr>
</tbody>
{% end %}
</table>
Run Code Online (Sandbox Code Playgroud)
我希望当我按下细节重定向/player_detail?username=username
并显示该播放器的所有细节时.我尝试使用href="javascript:window.location.replace('./player_info');"内部输入标记,但不知道如何将结果['用户名']放入.如何做到这一点?
我的Python程序中有这个函数:
@tornado.gen.engine
def check_status_changes(netid, sensid):
como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), '&sensid=', str(sensid), '&start=-5s&end=-1s'])
http_client = AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, como_url)
if response.error:
self.error("Error while retrieving the status")
self.finish()
return error
for line in response.body.split("\n"):
if line != "":
#net = int(line.split(" ")[1])
#sens = int(line.split(" ")[2])
#stype = int(line.split(" ")[3])
value = int(line.split(" ")[4])
print value
return value
Run Code Online (Sandbox Code Playgroud)
我知道
for line in response.body.split
Run Code Online (Sandbox Code Playgroud)
是一个发电机.但我会将值变量返回给调用该函数的处理程序.这可能吗?我能怎么做?
默认情况下,Tornado会在a Cache-Control: public提供的任何文件上放置标题StaticFileHandler.怎么能改成Cache-Control: no-cache?
我将在Python中实现Comet(尽管我听到了关于erlycomet的好消息,但我对于支持基于Erlang的Web服务器以及后端的其他内容并不感到激动).我发现了几种可能性:
(还有一些与Java服务器接口的其他选择,但我对那些不感兴趣)
考虑到性能,社区和易于实施,有人可以在这些实现中提出建议吗?
Tornadoweb和Nginx目前是流行的Web服务器,许多基准测试表明它们在某些情况下比Apache具有更好的性能.所以我的问题是:
'epoll'是使它们如此之快的最重要原因吗?如果我想编写一个好的套接字服务器,我还能从中学到什么?
请帮我创建HTTPS tornado服务器我当前的代码Python3不起作用
import os, socket, ssl, pprint, tornado.ioloop, tornado.web, tornado.httpserver
from tornado.tcpserver import TCPServer
class getToken(tornado.web.RequestHandler):
def get(self):
self.write("hello")
application = tornado.web.Application([
(r'/', getToken),
])
# implementation for SSL
http_server = tornado.httpserver.HTTPServer(application)
TCPServer(ssl_options={
"certfile": os.path.join("/var/pyTest/keys/", "ca.csr"),
"keyfile": os.path.join("/var/pyTest/keys/", "ca.key"),
})
if __name__ == '__main__':
#http_server.listen(8888)
http_server = TCPServer()
http_server.listen(443)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
HTTPS对我来说非常重要,请帮忙
在Tornado中,我们通常编写以下代码来异步调用函数:
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post(self):
...
yield self.handleRequest(foo)
...
@tornado.gen.coroutine
def handleRequest(self, foo):
...
Run Code Online (Sandbox Code Playgroud)
但是在asyncio中(将随Python 3.4一起提供,可以从pip for Python 3.3安装),我们yield from用来实现同样的目的:
@asyncio.coroutine
def myPostHandler():
...
yield from handleRequest(foo)
...
@asyncio.coroutine
def handleRequest(foo)
...
Run Code Online (Sandbox Code Playgroud)
从代码看,差异是yield和yield from.然而,前者handleRequest(foo)返回一个tornado.concurrent.Future对象,后者返回一个generator对象.
我的问题是,机制中两件事之间有什么区别?控制流程如何?谁调用实际handleRequest并检索其返回值?
附加:我具有Python生成器和迭代器的基本知识.我想通过使用这些来了解Tornado和asyncio的成就,以及这两种机制之间的区别.