使用BaseHTTP的基本身份验证卡住了Python HTTP服务器
我在尝试让一个基于Python的网页服务器工作时遇到了麻烦。
我想实现基本的身份验证(发送一个401的响应头),并根据用户列表进行验证。我可以顺利发送带有“WWW-Authorize”头的401响应,也能验证用户的响应(用户名和密码经过base64编码),但是在成功验证后,登录框还是一直弹出来。
import SimpleHTTPServer
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
''' Main class to present webpages and authentication. '''
def do_HEAD(self):
print "send header"
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
''' Present frontpage with user authentication. '''
self.do_HEAD()
if self.headers.getheader('Authorization') == None:
self.wfile.write('no auth header received')
pass
elif self.headers.getheader('Authorization') == 'Basic dGVzdDp0ZXN0':
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('authenticated!')
pass
else:
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('not authenticated')
pass
httpd = SocketServer.TCPServer(("", 10001), Handler)
httpd.serve_forever()
if __name__ == '__main__':
main()
在第一次加载(http://localhost:10001)时,登录框弹出来,我输入了正确的用户名和密码“test, test”,用户验证也通过了,但登录框又弹回来了。如果我点击取消,我就能进入验证后的页面……
有没有人能帮帮我?我怀疑这和授权是在do_GET函数下进行有关,因为每次加载页面时这个函数都会被触发。
2 个回答
7
这是因为你在回应时总是发送401状态码和WWW-Authenticate
这个头信息。其实你只需要在请求中没有合适的认证信息时才这么做。如果你对请求的内容满意,就发送200状态码(或者其他合适的状态码),然后就不需要再请求认证了。
21
试试这个:
import SimpleHTTPServer
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
''' Main class to present webpages and authentication. '''
def do_HEAD(self):
print "send header"
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
print "send header"
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
''' Present frontpage with user authentication. '''
if self.headers.getheader('Authorization') == None:
self.do_AUTHHEAD()
self.wfile.write('no auth header received')
pass
elif self.headers.getheader('Authorization') == 'Basic dGVzdDp0ZXN0':
self.do_HEAD()
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('authenticated!')
pass
else:
self.do_AUTHHEAD()
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('not authenticated')
pass
httpd = SocketServer.TCPServer(("", 10001), Handler)
httpd.serve_forever()
if __name__ == '__main__':
main()