我在使用python服务网页时遇到问题
我试图用python提供一个基本的网页。 如果我将cd刻录到dir,然后运行“python-mhttp.server80”,则显示的页面没有问题,所有格式都是正确的。问题是我需要某种程度的安全性,因此我编写了一个基本的身份验证python脚本,该脚本在同一目录下运行。不幸的是,我得到了一个没有格式的页面。有人能帮我吗?所有我需要做的是一旦完全认证,在当前目录下启动这个网站 代码是:-我在使用python服务网页时遇到问题,python,web-services,Python,Web Services,我试图用python提供一个基本的网页。 如果我将cd刻录到dir,然后运行“python-mhttp.server80”,则显示的页面没有问题,所有格式都是正确的。问题是我需要某种程度的安全性,因此我编写了一个基本的身份验证python脚本,该脚本在同一目录下运行。不幸的是,我得到了一个没有格式的页面。有人能帮我吗?所有我需要做的是一旦完全认证,在当前目录下启动这个网站 代码是:- import http.server import socketserver from http.server
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() '''
在这一点上,你真的应该考虑使用一个合适的web框架。你应该看看Django,因为你似乎想要一些最小的东西,一个简单的身份验证库应该可以做到这一点。