Search code examples
pythontwisted.webtwisted.internet

How to run the Twisted Matrix Web Server


I want to run the demo that is part of open source project JInfinote. The demo server is written using Twisted Matrix library/server. However I have no idea how to run this and if it's standalone server that I need to download in order to run or is it just python or a library and how to configure the whole thing.

When I try to run this in python I'm getting some session exception - as if it was trying to literally execute the code function by function. I would appreciate any help with this.

I am sorry for the level of this question, but I'm not a python programmer and I'm just trying to understand the project JInfinote and this is a blocker.


Solution

  • Well, in order to run twisted matrix on a web-server, all you have to do is really just run a simple Python Script:

    from twisted.web import server, resource
    from twisted.internet import reactor
    
    class HelloResource(resource.Resource):
        isLeaf = True
        numberRequests = 0
    
        def render_GET(self, request):
            self.numberRequests += 1
            request.setHeader("content-type", "text/plain")
            return "I am request #" + str(self.numberRequests) + "\n"
    
    reactor.listenTCP(80, server.Site(HelloResource()))
    reactor.run()
    

    If you listen on port 80 then your server is open to the web. You can learn more about it from here.