# -*- coding:cp949 -*- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler import base64 import sys class MyRequestHandler(BaseHTTPRequestHandler): def do_GET(self): try: addr, port = self.client_address print "Connection from", addr, "port", port print "Request header:" print self.headers urlpath = self.path if urlpath == '/status.html': if self.login_process() == True: self.send_response(200) self.send_header('Connection', 'Closed') self.send_header('Content-Type', 'text/html') self.end_headers() f = file('status.html', 'r') self.wfile.write(f.read()) f.close() else: self.response401() self.wfile.write('
Login Canceled.
') else: raise IOError() except IOError: self.send_error(404, 'File not found: %s' % self.path) def log_message(self, format, *args): pass # 로그 메시지 끄고 조용히 하기 위함 def login_process(self): if self.headers.has_key('Authorization'): authm, val = self.headers['Authorization'].split() userid, passwd = base64.b64decode(val).split(':') # print userid, passwd if userid == 'guest' and passwd == 'guestpass': return True return False def response401(self): self.send_response(401) self.send_header('WWW-Authenticate', 'Basic realm=\"pyrealm\"') self.send_header('Content-Type', 'text/html') self.send_header('Connection', 'Closed') self.end_headers() # 상태 보고 서버를 시작함 def StartStatusServer(port): httpd = HTTPServer(('', port), MyRequestHandler) try: print "Status server started." httpd.serve_forever() except KeyboardInterrupt: print "Status server finished." def main(argv) : if len(argv) != 2: print >> sys.stderr, "Usage: MonitorServer_02.py