import sysfrom twisted.internet import reactor, endpointsfrom twisted.web import serverfrom twisted.web.resource import Resourcefrom twisted.web.static import Datasys.path.append('lib')content = """<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><link rel="stylesheet" href="/static/test.css" type="text/css" /></head><body><span class='twistedTest'>This</span> is a test</body></html>"""class tServer(Resource):def render_GET(self, request):return bytes(content, "utf-8")if __name__ == "__main__":root = Resource()static_collection = Resource()static_collection.putChild(b"test.css", Data(b".twistedTest {color: red;}", "text/css"))root.putChild(b"static", static_collection)root.putChild(b"", tServer())site = server.Site(root)endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)endpoint.listen(site)reactor.run()print("Shutting down!")
import sysfrom twisted.internet import reactor, endpointsfrom twisted.web import serverfrom twisted.web.resource import Resourcecontent = """<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><link rel="stylesheet" href="/static/test.css" type="text/css" /></head><body><span class='twistedTest'>This</span> is a test</body></html>"""css = """.twistedTest {color: red;}"""class tServer(Resource):isLeaf = Truedef render_GET(self, request):if request.postpath == [b'']:request.setHeader("content-type", "text/html")return bytes(content, "utf-8")elif request.postpath == [b'static', b'test.css']:request.setHeader("content-type", "text/css")return bytes(css, 'utf-8')else:request.setResponseCode(404)return b'not found'if __name__ == "__main__":site = server.Site(tServer())endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)endpoint.listen(site)reactor.run()print("Shutting down!")
On Oct 31, 2018, at 2:15 PM, Jeff Grimmett <grimmtooth@gmail.com> wrote:Tried that, I get a big
No Such Resource
No such child resource.
back. Watching it in FF's development panel, I see a 404 come back for /. /static doesn't get served at all, of course.This, however, DID work.class tServer(Resource):
isLeaf = False
def getChild(self, path, request):
print('You know what you doing.')
if path == b'':
print("Rendering /")
return self
return Resource.getChild(self, path, request)
def render_GET(self, request):
return bytes(content, "utf-8")(ignore my printf debugging plz)So, Thanks! :)Regards,Jeff_______________________________________________On Tue, Oct 30, 2018 at 6:42 PM Donal McMullan <donal.mcmullan@gmail.com> wrote:Try replacing:isLeaf = TruewithisLeaf = False_______________________________________________On Tue, 30 Oct 2018 at 21:32, Jeff Grimmett <grimmtooth@gmail.com> wrote:_______________________________________________I'm sure I'm overlooking something obvious here but I just can't get my head around it.Here's the setup: twisted.web server that generates dynamic content. Child that serves up static content, e.g. css and favoicon. However, the static content isn't making it. Instead, any hit to localhost/static actually yields up a copy of / again.
Here's the server codeimport sys
from twisted.internet import reactor, endpoints
from twisted.web import server
from twisted.web.resource import Resource
from twisted.web.static import File
sys.path.append('lib')
content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/test.css" type="text/css" />
</head>
<body>
<span class='twistedTest'>This</span> is a test
</body>
</html>
"""
class tServer(Resource):
isLeaf = True
def render_GET(self, request):
return bytes(content, "utf-8")
if __name__ == "__main__":
root = tServer()
root.putChild(b"static", File("static"))
site = server.Site(root)
endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
endpoint.listen(site)
reactor.run()
print("Shutting down!")It's run with the command 'python tserver.py'. The expectation is that what is inside the custom <span> will be red.In the same dir as the script is a subdir 'static' with the css file inside it.If I replace 'root' with root = Resource() then / doesn't serve up anything, but /static is a directory listing of the static directory.The dynamic server is basically a copy of several tutorials cooked down to something that I could use to demonstrate the problem.What am I missing here? /headscratch
Regards,Jeff
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python