我有一个python服务器,它提供页面。使用IdP(身份提供者)登录这些页面。所以当用户试图打开我的服务器页面时,他会被重定向到IdP页面。与此同时,我的服务器开始监听IdP响应。当用户登录到inidp页面时,IdP向我的服务器发送数据,服务器处理这些数据并向用户显示我的页面。在
问题是,如果多个客户机尝试登录,当服务器收到来自一个IdP的第一个响应时,所有用户都将使用第一个用户的凭据将我的页面显示为已登录。在
我的意思是,当服务器开始监听时,它会等待所有人。当第一个用户登录时,所有等待登录的用户都具有相同的凭据。在
我怎样才能解决这个大问题?多线程可能有帮助?在
这里有一些重要的代码。假设我想将idp回答的一些数据加载到页面登录表单的“name”字段中。在
class Login(django.forms.SelfHandlingForm):
def __init__(self, *args, **kwargs):
super(Login, self).__init__(*args, **kwargs)
response = self.getIdPResponse()
self.fields['name'].widget=forms.TextInput(attrs={'value': response['name']})
def getIdPResponse(self):
global response
response = None
timeout = 300
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
global response
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
varLen = int(self.headers["Content-Length"])
#response = urlparse.parse_qs(self.rfile.read(varLen))
response = self.rfile.read(varLen)
self.wfile.write("Log-in ok! now you can close this page.")
httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', 8080), RequestHandler)
try:
httpd.socket.settimeout(1)
except BaseException as e:
print e
count = 0
'''at this point, the IdP page is already showed to the user '''
while response is None and count < timeout:
try:
httpd.handle_request()
count = count + 1
print 'waiting for the Idp answer...'
except Exception as e:
print e
return response
这样,当用户成功登录时,所有等待登录的用户都将在“name”字段中显示第一次登录的用户的名称。显然我不想这样。在
一种方法是使用事件驱动系统,如twisted,将
getIdPResponse
方法分成两部分:这样,您的单个线程就不会阻塞,并且可以同时为其他客户机分派事件。在
如果你想简化你的应用程序逻辑,你可以在这上面包装一层阻塞调用,但我不推荐这样做。在
相关问题 更多 >
编程相关推荐