小编Ale*_*der的帖子

使用BaseHTTP使用基本身份验证的Python HTTP Server

我试图让一个基于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()
Run Code Online (Sandbox Code Playgroud)

在第一次加载(http:// localhost:10001)时,登录框弹出,我输入test,test(正确的用户)用户验证确定,但是框弹出,如果我单击取消,我将进入验证页面. .. …

python http-authentication basehttpserver

10
推荐指数
2
解决办法
2万
查看次数