This article mainly introduces Python's simple web server based on twisted, which can simulate simple web server functions and is a very practical technique, for more information, see the following example. The specific method is as follows:
1. create an htm folder and add the displayed webpage files to the folder.
2. create web. py in the directory of the same level as the htm folder. the content of web. py is:
from twisted.web.resource import Resource from twisted.web import server from twisted.web import static from twisted.internet import reactor PORT = 1234 ######################################################################## class ReStructed(Resource): """""" #---------------------------------------------------------------------- def __init__(self, filename, *a): """Constructor""" self.rst = open(filename).read() def render(self, request): return self.rst resource = static.File('htm/') resource.processors = {'.html':ReStructed} resource.indexNames = ['index.html'] reactor.listenTCP(PORT, server.Site(resource)) reactor.run()
3. install twisted for: http://twistedmatrix.com/trac/
Install zope module: http://old.zope.org/Products/ZopeInterface/3.3.0/zope.interface-3.3.0.tar.gz/swreleasefile_view
5. run python web. py in the command line (windows system ).
6. enter 127.0.0.1: 1234 in the browser to see the effect as shown in:
I hope this article will help you with Python programming.