python 反向代理怎么增加一个用户验证

海乃百川 2019-07-26 02:10:03
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import BaseHTTPServer
import hashlib
import os
import urllib2

class CacheHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
m = hashlib.md5()
m.update(self.path)
cache_filename = m.hexdigest()
if os.path.exists(cache_filename):
print "Cache hit"
data = open(cache_filename).readlines()
else:
print "Cache miss"
data = urllib2.urlopen("http://127.0.0.1:9001" + self.path).readlines()
open(cache_filename, 'wb').writelines(data)
self.send_response(200)
self.end_headers()
self.wfile.writelines(data)

def run():
server_address = ('', 8001)
httpd = BaseHTTPServer.HTTPServer(server_address, CacheHandler)
httpd.serve_forever()

if __name__ == '__main__':
run()

代码如上,127.0.0.1:9001 是一个kafka-manager,我使用python做了一个反向代理,怎么样给这段脚本增加一个用户验证,即只有用户验证成功才可以访问后端的kafka-manager。
...全文
80 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄哥Python培训 2019-07-26
  • 打赏
  • 举报
回复
再参考一个代码 https://github.com/goya191/SimpleAuthServerSSL.py/blob/master/SimpleHttpsAuthServer.pyhttps://github.com/goya191/SimpleAuthServerSSL.py/blob/master/SimpleHttpsAuthServer.py
黄哥Python培训 2019-07-26
  • 打赏
  • 举报
回复
请参考这个代码 https://gist.github.com/singe/cba85800dd6e701c53d0614d8506b281https://gist.github.com/singe/cba85800dd6e701c53d0614d8506b281

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧