This article describes Python based on twisted to implement a simple Web server, share for everyone to reference. The specific methods are as follows:
1. New HTM folder, put the displayed page file in this folder
2. At the same level as the HTM folder, the web.py,web.py content is established as follows:
From Twisted.web.resource import resource to
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 the Twisted download address is: http://twistedmatrix.com/trac/
Installing the Zope module: Http://old.zope.org/Products/ZopeInterface/3.3.0/zope.interface-3.3.0.tar.gz/swreleasefile_view
5. Run on the command line (Windows system): Python web.py
6. In the browser input: 127.0.0.1:1234, see the effect as shown in the following image:
I hope this article will help you with your Python programming.