Skip to content

Instantly share code, notes, and snippets.

@Parth-Vader
Last active August 15, 2017 18:32
Show Gist options
  • Save Parth-Vader/2cdae238cc44c132e63c0381fdc542de to your computer and use it in GitHub Desktop.
Save Parth-Vader/2cdae238cc44c132e63c0381fdc542de to your computer and use it in GitHub Desktop.
A mock server to run books site
from twisted.internet import reactor, endpoints
from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
import time
import os
class Home(Resource):
isLeaf = False
def __init__(self, pathname):
Resource.__init__(self)
self.pathname = pathname
def getChild(self, name, request):
if name == '':
return self
return Resource.getChild(self, name, request)
def render_GET(self, request):
path = "/var/www/html/books.toscrape.com/"
fname = path + self.pathname
if (".html" or ".ico") in self.pathname:
f = open(fname)
s=f.read()
return s
else:
fname = fname + "/index.html"
f = open(fname)
s=f.read()
return s
class Catalogue(Resource):
def getChild(self,name,request):
return first(name)
class first(Resource):
def __init__(self, pathname):
Resource.__init__(self)
self.pathname = pathname
def getChild(self,name,request):
name1 = self.pathname + "/" + name
return second(name1)
class second(Resource):
def __init__(self, pathname):
Resource.__init__(self)
self.pathname = pathname
def getChild(self,name,request):
name1 = self.pathname + "/" + name
return Home(name1)
resource = Catalogue()
factory = Site(resource)
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
endpoint.listen(factory)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment