Python implements simple web Server Based on twisted, pythontwistedweb

Source: Internet
Author: User
Tags django website

Python implements simple web Server Based on twisted, pythontwistedweb

The example in this article describes Python's simple web Server Based on twisted, and shares it with you for your reference. 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.


Daxia recommends several examples of open-source python projects.

There are many Python open-source projects.
For example, Twisted and Tornado are used as web servers.
WxPython GUI
Django website creation
And NumPy for scientific computing.
These are all very large. There are many other small and medium-sized enterprises that can be searched on GitHub.
In addition, there are some Python-based websites, such as the famous Douban, zhihu, and shell.
In addition, many web games are implemented in Python at the backend. Although these are not open-source, basic technologies can be found in open-source projects.

Why does the python twisted write tcp client server accept the message when selftransportwrite transmits multiple data?

Because tcp socket only transmits a continuous data stream, if you write:
Self. transport. write ('Z') self. transport. write ('Z') self. transport. write ('Z') because the three writes are continuous, for socket, it is to transmit a piece of data such as '5a 5a 5a 'represented by hex, there is no difference between Writing separately or writing once for socket.
The other end of the tcp socket needs to accept the request multiple times. Either the data you want to transmit is too large, a packet cannot be installed, or the time interval between the two transfers is exceeded, two packages are formed.
Generally, you need another Application Layer protocol. For example, you can use \ r \ n to separate each write, then, when receiving the data, you can find \ r \ n in the data, and then you can process each write separately.


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.