Python implements simple web servers based on twisted

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.