كيفية استخدام Cherrypy كخادم ويب للملفات الثابتة؟
سؤال
هل هي طريقة سهلة لاستخدام Cherrypy كخادم ويب سيعرض .html
الملفات في بعض المجلد؟ تنص جميع الوثائق التمهيدية في Cherrypy على أن المحتوى الذي تم إنشاؤه ديناميكيا:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
هل هي أي طريقة سهلة للاستخدام index.html
بدلا من طريقة Helloworld.Index ()؟
المحلول
سيقدم هذا الرمز البسيط الملفات في الدليل الحالي.
import os
import cherrypy
PATH = os.path.abspath(os.path.dirname(__file__))
class Root(object): pass
cherrypy.tree.mount(Root(), '/', config={
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': PATH,
'tools.staticdir.index': 'index.html',
},
})
cherrypy.quickstart()
نصائح أخرى
فيما يلي بعض المعلومات حول خدمة المحتوى الثابت مع Cherrypy: http://docs.cherrypy.org/stable/progguide/files/static.html.
راجع للشغل، إليك طريقة بسيطة لمشاركة الدليل الحالي عبر HTTP مع Python:
# بيثون 3 $ python -m http.server [port] # python 2 $ python -m simitehttpserver [port
# encode: utf-8
import cherrypy
WEB_ROOT = "c:\\webserver\\root\\"
class CServer( object ) :
@cherrypy.expose
def do_contact(self, **params):
pass
cherrypy.server.socket_port = 80
# INADDR_ANY: listen on all interfaces
cherrypy.server.socket_host = '0.0.0.0'
conf = { '/':
{ 'tools.staticdir.on' : True,
'tools.staticdir.dir' : WEB_ROOT,
'tools.staticdir.index' : 'index.html' } }
cherrypy.quickstart( CServer(), config = conf )
لا تنتمي إلى StackOverflow