gpt4 book ai didi

python - 从 RequestHandler 继承的类中缺少 self.async_callback

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:06 30 4
gpt4 key购买 nike

所以我现在正在学习 Tornado 网络框架,并按照书中的几个示例成功地获得了 OAuth2/Google 的身份验证,几乎可以正常工作。它重定向到 Google 以提示用户登录,但在登录后它向我抛出以下错误:

Traceback (most recent call last):
File "C:\python27\lib\site-packages\tornado\web.py", line 1288, in _stack_context_handle_exception
raise_exc_info((type, value, traceback))
File "C:\python27\lib\site-packages\tornado\web.py", line 1475, in wrapper
result = method(self, *args, **kwargs)
File "C:\Users\enricojr\Downloads\Github\LearningTornado\grumble_login.py", line 8, in get
self.get_authenticated_user(callback=self.async_callback(self._on_auth))
AttributeError: 'LoginHandler' object has no attribute 'async_callback'

在 Github 上查看 Tornado 源代码后,似乎确实没有使用 async_callback() 方法。关于如何处理这个的任何想法?到目前为止,我看到的每个使用 Tornado 的 auth 模块进行身份验证的示例都要求使用它,并且由于我对整个“异步 Web 编程”一无所知,所以我现在不太确定我还能做什么

我的代码如下,Python 2.7 和 Tornado 4.0.2:

############################
# grumble_login.py
############################
from tornado.auth import GoogleMixin
from tornado.web import RequestHandler, asynchronous

class LoginHandler(RequestHandler, GoogleMixin):
@asynchronous
def get(self):
if self.get_argument("openid.mode", None):
self.get_authenticated_user(callback=self.async_callback(self._on_auth))
return
else:
self.authenticate_redirect()

def _on_auth(self, user):
if not user:
self.clear_all_cookies()
raise tornado.web.HTTPError(500, "Google auth failed.")
else:
# the user id serves as the basis for
# all the data we store.
self.set_secure_cookie('user_id', user['id'])
self.redirect("/")

class LogoutHandler(RequestHandler):
def get(self):
self.clear_all_cookies()
self.render("logout.html")

############################
# main.py
############################
import os

from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application
from tornado.options import define, options

from grumble_handlers import *
from grumble_user_handlers import *
from grumble_login import *


# /user/profile/<user> - leads to profile page
# /user/posts - leads to posts made by user

# /post/<id> - leads to a specific post
# /post/add - add post
# /post/edit - edit post
# /post/delete - delete post
# D$oP5lz3D$oP5lz3

TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "templates")
STATIC_PATH = os.path.join(os.path.dirname(__file__), "static")

if __name__ == "__main__":
define("port", default=8000, help="run on the given port", type=int)

settings_dict = {
'cookie_secret': "GENERATE NEW KEY HERE",
'template_path': TEMPLATE_PATH,
'static_path': STATIC_PATH,
'debug': True,
'login_url': "/login",
}

general_handlers = [
(r"/", IndexHandler),
(r"/login", LoginHandler),
(r"/logout", LogoutHandler)
]

post_handler_list =[
(r"/post/(\d+)", PostFetchHandler), # leads to a single post page
(r"/post/add", PostAddHandler),
(r"/post/edit", PostEditHandler),
(r"/post/delete", PostDeleteHandler),
]

user_handler_list = [
(r"/user/(\d+)/posts", UserPostsHandler), # lists out all the posts by a given user
(r"/user/(\d+)/profile", UserProfileHandler),
]

master_handler_list = general_handlers + post_handler_list + user_handler_list

app = Application(
handlers=master_handler_list,
**settings_dict
)

http_server = HTTPServer(app)
http_server.listen(options.port)

# GO TEAM GO
IOLoop.instance().start()

############################
# grumble_handlers.py
############################
...
class IndexHandler(NotImplementedHandler):
@authenticated
def get(self):
pass

最佳答案

不幸的是,这本书早就过时了。 async_callback was removed in Tornado 4 since it is no longer required .你可以简单地做:

self.get_authenticated_user(callback=self._on_auth)

一旦你开始工作,我建议关注 the coroutine documents并学习如何使用“yield”:

user = yield self.get_authenticated_user()

关于python - 从 RequestHandler 继承的类中缺少 self.async_callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467606/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com