在upd.你重写你的问题?在
cgi将http响应打印到stdout。
首先你需要得到标题。。。如果标题“Authorization”存在,您可以从中解码登录名和密码。
如果没有无效的标题或密码,您必须:
发送401响应
发送包含“Basic realm=\“Message\”的“WWW Authenticate”头进行基本授权。也可能是文摘,Ntlm等
如果浏览器得到401响应,它会要求用户输入密码(即使是ajax),用服务器要求的方法对登录名和密码进行编码,并用“Authorization”头再次发送请求。在
不是CGI代码示例,HTTPServer的decorator,BaseHTTPRequestHandlerdef login_required(f):
def authenticate(self,*args,**kwargs):
if conf.get('noauth'):
return f(self,*args,**kwargs)
if self.headers.getheader('Authorization') :
cred = base64.b64decode(self.headers.getheader('Authorization').split(' ')[1])
users = conf.get('users', [])
if cred in users:
self.creds = cred
return f(self,*args,**kwargs)
#else
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Meter\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Not authenticated.')
return False
return authenticate