Python implements a simple Web server based on twisted _python

Source: Internet
Author: User

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.

Related Article

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.