我的目标是为我的家庭网络制作一个简单的网络服务器,我可以用来存储文件,允许我将很少使用的文件保存在我的计算机上,然后在需要时下载它们。现在我还在努力展示html页面。我想出了如何显示一个名为“upload.html”和一个名为“index.html”的名称。但是我有一个名为“dataSync.html”的页面,浏览器只显示html文件的所有文本,前面有“”。这些网页不是最终的网页,它们只是测试,所以我可以在制作网页之前让python服务器正常工作。 “dataSync.html”实际上是我的第一个网页,仅用于测试目的。
这是python代码:
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
class MyHandler(BaseHTTPRequestHandler):
def __set_headers__(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self.__set_headers__()
def do_GET(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
try:
if self.path.endswith('.html'):
f = open(dir_path + self.path) # open requested file
# send code 200 response
self.send_response(200)
# send header first
self.send_header('Content-type', 'text-html')
self.end_headers()
# send file content to client
self.wfile.write(f.read().encode('utf-8'))
f.close()
return
except IOError:
self.send_error(404, 'file not found')
def main():
try:
httpserver = HTTPServer(('', 8888), MyHandler)
print('started httpserver...')
httpserver.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down server')
httpserver.socket.close()
if __name__ == '__main__':
main()
索引代码:
<!doctype html>
<html>
<head>
<title>Our Funky HTML Page</title>
<meta name="description" content="Our first page">
<meta name="keywords" content="html tutorial template">
</head>
<body>
Content goes here.
</body>
</html>
DataSynce代码:
<!DOCTYPE html>
<html>
<head>
<title>Data Sync</title>
<link rel="stylesheet" href="data sync.css">
<script>
function hide() {
document.getElementById("text").style.visibility = "hidden";
}
</script>
<script>
function show() {
document.getElementById("text").style.visibility = "visible";
}
</script>
</head>
<body>
<h1>Data Sync</h1>
<p>Here you will be able to sync your data between two computers</p>
<img src="https://qbservices.net/wp-content/uploads/2015/07/quickbooks-sync-
300x94.jpg">
<p id="text">click the button to change this text</p>
<button id="button1" onclick="hide()">Hide</button>
<button id="button2" onclick="show()">Show</button>
<button id="FolderButton"
onClick="document.querySelector('.inputFile').click();">Folder</button>
<input class="inputFile" type="file" accept=".dir" style="display: none"
webkitdirectory directory multiple/>
</body>
</html>
dataSync.html在python之外工作 任何帮助都会很棒,谢谢。