2012-12-29 26 views
15

Tôi đã cố triển khai phiên webapp2 của GAE, nhưng dường như có rất ít tài liệu về nó. Theo http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html, các bước của tôi như sau:Phiên webapp2 GAE: quy trình tạo và kiểm tra phiên chính xác

1.Configure và thêm cấu hình cho các ứng dụng chính:

config = {} 
config['webapp2_extras.sessions'] = { 
    'secret_key': 'my_secret_key', 
} 
app = webapp2.WSGIApplication([...], config=config) 

phiên 2.Create trong xử lý đăng nhập

# Delete existent session 
    --> not mention in the tutorial 
# member is found  
self.session_store = sessions.get_store(request=handler.request) 
self.session['account'] = member.account 

3.Check nếu một phiên tồn tại ở các vị trí khác nhau trong chương trình của tôi

if self.session['account']: 
    # Session exists 

4.Xóa lệnh ion khi người dùng đăng xuất

--> not mentioned in the tutorial 

Câu hỏi của tôi:

  1. tôi đã thông báo lỗi "... đối tượng không có thuộc tính 'phiên'" trong quá trình tạo phiên (Bước 2)

  2. Làm cách nào để xóa phiên trong bước 2 và 4?

  3. Quy trình quản lý phiên tổng thể có chính xác không?

Cảm ơn.

Trả lời

5

Đây không phải là câu trả lời trực tiếp cho câu hỏi, nhưng đó là giải pháp tôi đã tìm thấy bằng cách sử dụng gaesessions thay vì phiên webapp2 của GAE và tôi muốn chia sẻ với ybody. Ở đây chúng tôi đi:

  1. Tải xuống gaesessions từ https://github.com/dound/gae-sessions bằng cách nhấp vào nút "Tải xuống ZIP". Tệp được tải xuống là "gae-sessions-master.zip".

  2. Giải nén tệp (thư mục "gae-sessions-master" sẽ được tạo) và sao chép thư mục "gaessions" vào thư mục gốc của ứng dụng của bạn (ví dụ: "app.yaml" là)

  3. Tạo tệp có tên "appengine_config.py" trong thư mục gốc, với các nội dung sau (hình thức sao chép https://github.com/dound/gae-sessions/tree/master/demo):

    from gaesessions import SessionMiddleware 
    
    # Original comments deleted ... 
    # Create a random string for COOKIE_KDY and the string has to 
    # be permanent. "os.urandom(64)" function may be used but do 
    # not use it *dynamically*. 
    # For me, I just randomly generate a string of length 64 
    # and paste it here, such as the following: 
    
    COOKIE_KEY = 'ppb52adekdhD25dqpbKu39dDKsd.....' 
    
    def webapp_add_wsgi_middleware(app): 
        from google.appengine.ext.appstats import recording 
        app = SessionMiddleware(app, cookie_key=COOKIE_KEY) 
        app = recording.appstats_wsgi_middleware(app) 
        return app 
    
  4. Tạo một phiên khi người dùng đăng nhập (biến tài khoản là tài khoản của người dùng):

    from gaesessions import get_current_session 
    session = get_current_session() 
    if session.is_active(): 
        session.terminate() 
    # start a session for the user (old one was terminated) 
    session['account'] = account 
    
  5. Kiểm tra xem phiên làm việc của người dùng tồn tại, nếu tài khoản có, lợi nhuận của người dùng:

    from gaesessions import get_current_session 
    def checkSession(): 
        session = get_current_session() 
        if session.is_active(): 
         return session['account'] 
        return False 
    
  6. Xóa phiên khi người dùng đăng xuất:

    def logout(): 
        session = get_current_session() 
        if session.is_active(): 
         session.terminate() 
    
  7. Cuối cùng, bạn có thể tạo một công việc định kỳ để làm sạch phiên hết hạn định kỳ:

cron.yaml:

- description: daily session cleanup 
    url: /clean_up_sessions 
    schedule: every day 3:00 
    timezone: ... (Your time zone) 

Chức năng:

from gaesessions import delete_expired_sessions 
class clean_up_sessions(webapp2.RequestHandler): 
    def get(self): 
     while not delete_expired_sessions(): 
      pass 

Hy vọng điều này sẽ giúp.

+1

Tại sao sử dụng gae-sessions thay vì webapp2_extras.sessions? gae-sessions so sánh chính nó với một vài hệ thống phiên nhưng không so sánh với các phiên của webapp2. – Romz

+0

Cảm ơn rất nhiều, Romz. Tôi không biết rằng có webapp2_extras.sessions. Tôi sẽ thử. –

15

Dưới đây là một ví dụ về xử lý và làm thế nào để sử dụng webapp2 phiên thêm

main.py với BaseHandler và MainHandler

import webapp2 
from webapp2_extras import sessions 

class BaseHandler(webapp2.RequestHandler):    # taken from the webapp2 extrta session example 
    def dispatch(self):         # override dispatch 
     # Get a session store for this request. 
     self.session_store = sessions.get_store(request=self.request) 

     try: 
      # Dispatch the request. 
      webapp2.RequestHandler.dispatch(self)  # dispatch the main handler 
     finally: 
      # Save all sessions. 
      self.session_store.save_sessions(self.response) 

    @webapp2.cached_property 
    def session(self): 
     # Returns a session using the default cookie key. 
     return self.session_store.get_session() 

class YourMainHandler(BaseHandler): 

    def get(self): 

     .... 
     self.session['foo'] = 'bar' 


    def post(self): 


     foo = self.session.get('foo') 

Và nếu bạn có một login.py riêng biệt:

.... other imports 
import main 

class Login(main.BaseHandler): 

    def get(self): 

     .... 
     self.session['foo'] = 'bar' 


    def post(self): 


     foo = self.session.get('foo') 
+0

Đánh giá cao sự giúp đỡ của bạn rất nhiều. Tôi vẫn đang vật lộn, nhưng không có may mắn. Giả sử tôi có một mô-đun "login.py" với lớp Đăng nhập (webapp2.RequestHandler) trong đó để xử lý đăng nhập người dùng. Ngoài ra còn có một mô-đun chính "main.py" với lớp MainPage (BaseHandler) trong đó. Làm cách nào để thay đổi lớp Đăng nhập? Tôi nhập chính trong login.py và thay đổi lớp Đăng nhập (webapp2.RequestHandler) thành Đăng nhập (main.BaseHandler). Thông báo lỗi: đối tượng 'module' không có thuộc tính 'BaseHandler' –

+0

Tôi đã cập nhật câu trả lời. – voscausa

+0

Đó là những gì tôi đã nói về: thông báo lỗi: đối tượng 'module' không có thuộc tính 'BaseHandler' –

3

Trong RequestHandler bạn override dispatch: từ phiên nhập khẩu webapp2_extras

def dispatch(self): 

    self.session_store = sessions.get_store(request=self.request) 

    try: 
     webapp2.RequestHandler.dispatch(self) 
    finally: 
     self.session_store.save_sessions(self.response) 

và thực hiện một webapp2.cached_property gọi session:

@webapp2.cached_property 
def session(self): 
    return self.session_store.get_session(backend="<whatever you want here>") 

Khi bạn muốn truy cập các giá trị session, bạn làm self.session[<key>]

Khi người dùng đăng nhập, bạn có thể gọi một trong hai:

self.auth.get_user_by_password(auth_id, password, remember=True, 
              save_session=True) 

mà sẽ chăm sóc loại bỏ các phiên cũ và tạo ra cái mới cho bạn, hoặc:

self.auth.set_session(self.auth.store.user_to_dict(self.user), remember=True) 

Theo như đăng xuất, tất cả các bạn nên cần để gọi là:

self.auth.unset_session() 
+0

'self.auth' là gì? –

Các vấn đề liên quan