Comments (12)
I have a similar experience. I thought that returning an absolute path to a file served on / would work, but didn't. Solved it by:
@app.route('/')
def bootstrap(self, request):
f = static.File(get_assets_path('html'))
f.indexNames = ['bootstrap.html']
return f
from klein.
This is not a problem unique to klein.
Consider the following invocation of twistd's web plugin:
$ echo "Hello, world!" > hello.txt
$ twistd -n web --path hello.txt
2014-03-14 13:00:24-0700 [-] Log opened.
2014-03-14 13:00:24-0700 [-] twistd 12.2.0 (/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python 2.7.5) starting up.
2014-03-14 13:00:24-0700 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2014-03-14 13:00:24-0700 [-] Site starting on 8080
2014-03-14 13:00:24-0700 [-] Starting factory <twisted.web.server.Site instance at 0x10d675488>
2014-03-14 13:00:26-0700 [HTTPChannel,0,127.0.0.1] 127.0.0.1 - - [14/Mar/2014:20:00:26 +0000] "GET / HTTP/1.1" 404 145 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36"
You'll notice that GET /
always fails with a 404
.
from klein.
Is this addressed by using the same technique as in #59? I.E., something like this:
...
UPDATE: The technique from #59 seems to work for me:
from klein import Klein
from twisted.web.static import File
app = Klein()
@app.route('/', branch=True)
def root(request):
f = File(__file__)
f.isLeaf = True
f.type, f.encoding = 'text/plain', None # force MIME type for browser rendering
return f
if __name__ == '__main__':
app.run('localhost', 8080)
from klein.
So, did PR #59 fix this?
from klein.
Used the original reporter's code to reproduce this with Klein version 15.1.
@dreid: Would you say this is a bug in Klein's code, a quirk that the docs should explain, or neither?
from klein.
It's an issue with twisted.web.static.File
.
from klein.
So I'm hearing "the thing to do about it on the Klein side is note it in the docs, then remove the note when it's fixed in Twisted"?
I'm inclined to hear that because I'm working on another PR that is about updating and expanding Klein's docs.
from klein.
I think that is a worthwhile approach even if @hawkowl or @glyph (or anyone else really) decides to address it in Klein. (Note: I know longer work on Klein due to lack of time and no longer using it in my job.)
If someone wanted to make it "just work" the approach would probably be to ask the static.File
if it is a file or a directory. And set isLeaf if it is a file. (static.File
provides IFilePath
)
from klein.
Ah, thanks for clarifying your role—mostly @-mentioned you because you were the last contributor in the thread. Okay, I'll make a note about this behavior in the docs update branch and nudge the @hawkowl about this issue after that PR's sorted.
from klein.
Don't ask me why, but that works:
@app.route("/")
def index(self, _request):
return File(path_to_index_html)
@app.route("/index.html")
def index(self, _request):
return File(path_to_index_html)
if the two functions does not have the same name, it does not work
from klein.
@stibbons I'm not exactly sure why it works, but that is taking advantage of a bug in the way that we look up routes in werkzeug's url_map; it probably won't work forever :).
The workaround from @posita up-thread will work fine.
Or, perhaps to illustrate the issue more succinctly:
from klein import Klein
from twisted.web.resource import Resource
from twisted.web.static import File
app = Klein()
@app.route('/')
def root(request):
f = File(__file__)
f.type, f.encoding = 'text/plain', None
r = Resource()
r.putChild(b"", f)
return r
if __name__ == '__main__':
app.run('localhost', 8080)
from klein.
In my case I have the same issue.
I solved it by using '' as root path
resource.putChild('', File(....))
from klein.
Related Issues (20)
- klein.test.test_form.TestForms.test_cookieWithToken fails with treq 22.1.0 HOT 1
- Worst In Class Performance HOT 1
- rename main branch from master to trunk HOT 1
- extract requirements from tox.ini
- no contact info? HOT 1
- hyperlink generates invalid URLs via hypothesis which sometimes causes our tests to fail HOT 6
- KleinResourceTests.test_addSlash fails on Musl or glibc based system HOT 10
- Death to you garbage mainstream propaganda slaves
- Twisted fools, hope you have a tragic life and painful death.
- AttributeError: module 'OpenSSL.SSL' has no attribute 'TLS_METHOD' HOT 2
- Fix CI
- Klein does not work with Werkzeug 2.3
- Python 3.11 support
- Why "405 Method Not Allowed" ? HOT 3
- ReadTheDocs is showing old version of docs HOT 2
- release instructions as written do not actually issue a prerelease
- (maybe) put hypothesis back HOT 4
- the merge queue needs help from our status checks to actually work
- tox 4 usually hangs when I run tests locally HOT 1
- Returning a static file from a route that matches all URLs behaves unexpectedly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from klein.