2
我有以下代碼。它適用於index.html或位於home /目錄中的任何文件,但不會提供任何位於嵌套目錄中的文件,例如home/image/xyz.png。使用Bottle.py服務嵌套的靜態文件
import os
import sys
import ctypes
import pprint
import bottle
import socket
from bottle import *
#global functions
def ReadStringFromFile(file):
f = open(file, 'r')
data = f.read()
f.close()
return data
def getBits():
#get the system bits
sysBit = (ctypes.sizeof(ctypes.c_voidp) * 8)
if((not(sysBit == 32)) and (not (sysBit == 64))):
sysBit = 0
return sysBit
class DevelServer(object):
addr = ''
def __init__(self):
self.addr = socket.gethostbyname(socket.gethostname())
pass
def index(self):
return ReadStringFromFile('home/index.html')
#these are not the URLs you are looking for
def error404(self, error):
return '<br><br><br><center><h1>Go <a href="/">home</a>, You\'re drunk.'
def send_static(self, filename):
print (static_file(filename, root=os.path.join(os.getcwd(), 'home')))
return static_file(filename, root=os.path.join(os.getcwd(), 'home'))
#start hosting the application
def startThis(self):
bottle.run(host=self.addr, port=80, debug=True)
#instatiate the main application class an initalize all of the app routes
def Main():
ThisApp = DevelServer()
bottle.route('/')(ThisApp.index)
bottle.route('/home/<filename>')(ThisApp.send_static)
bottle.error(404)(ThisApp.error404)
ThisApp.startThis()
Main()
在你回覆之前,我其實已經想清楚了,對不起,花了這麼長時間纔回到你身邊,但這是正確的答案,非常感謝。 – Michael
沒問題 - 快樂你解決它!並感謝接受。 –