我在使用python服务网页时遇到问题

时间:2014-07-03 16:34:02

标签: python web-services

我正在尝试在python中提供基本网页。 如果我cd到dir然后运行" python -m http.server 80"页面显示没有问题,所有格式都正确。问题是我需要一定程度的安全性,所以我编写了一个基本的身份验证python脚本,我从同一个目录运行。不幸的是,我得到了一个没有格式化的糟糕渲染页面。有谁能够帮我?我需要做的就是完成身份验证后,在当前目录中启动此网站。

代码是: -

import http.server
import socketserver
from http.server import BaseHTTPRequestHandler, HTTPServer

class CustomHandler(BaseHTTPRequestHandler):
    ''' Main class to present webpages and authentication. '''
    def do_HEAD(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        print ('all authenticated')
        self.end_headers()

    def do_AUTHHEAD(self):
        self.send_response(401)
        self.send_header('WWW-Authenticate', 'Basic realm=\"SmarterADM\"')
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        ''' Present frontpage with user authentication. '''
        if self.headers['Authorization'] == None:
            self.do_AUTHHEAD()
            self.wfile.write(bytes('no auth header received', 'UTF-8'))
            pass
        elif self.headers['Authorization'] == 'Basic c21hcnRlcmFkbTpwYXNzdzByZA==':
             self.do_HEAD()
             self.wfile.write(bytes(self.headers['Authorization'], 'UTF-8'))
             self.wfile.write(bytes(' authenticated!', 'UTF-8'))
             rootdir = 'c:/Team/main/START/index.html' #file location
             f = open(rootdir,'rb') #open requested file
             self.wfile.write(f.read())
             return render.rootdir
             pass
        else:
             self.do_AUTHHEAD()
             self.wfile.write(bytes(self.headers['Authorization'], 'UTF-8'))
             self.wfile.write(bytes(' not authenticated Mark says NO!', 'UTF-8'))
             pass

httpd = HTTPServer(('', 80), CustomHandler)
print ('started httpd...')
httpd.serve_forever()

if __name__ == '__main__':
        main()
''''         rootdir = 'c:/RTS_Team/STAR2/STAR2/index.html' #file location
        f = open(rootdir,'rb') #open requested file
        self.wfile.write(f.read())
        f.close() '''

0 个答案:

没有答案