简体   繁体   English

web.py找不到文件

[英]web.py doesn't find file

I try to create a little website using web.py and webpysocketio, and I have a problem: Web.py doesn't seem to find any file besides the index.html. 我尝试使用web.py和webpysocketio创建一个小的网站,但是我遇到一个问题:Web.py除了index.html之外似乎找不到任何文件。

Here's my webpy app: 这是我的webpy应用程序:

import web
from socketio import SocketIOServer
from gevent import monkey
monkey.patch_all()
from webpy_socketio import *

urls = (
    '/', 'index',
)

urls += socketio_urls

app = web.application(urls, globals())

SOCKETIO_HOST = ""
SOCKETIO_PORT = 8080

application = app.wsgifunc()



if __name__ == "__main__":
       SocketIOServer((SOCKETIO_HOST, SOCKETIO_PORT), application, resource="socket.io").serve_forever()

class index:
    def GET(self):
        render = web.template.render('templates/')
        return render.index()

@on_message(channel="my channel")
def message(request, socket, context, message):
    socket.send_and_broadcast_channel(message)

In my template folder I have the index.html ( and the socketio_scripts.html ): 在我的模板文件夹中,我有index.htmland the socketio_scripts.html ):

<html>
  <head>
    <meta charset="utf-8">
    <title>webpysocketio TEST</title>
    <object type="text/html" data="socketio_scripts.html">
        <script>
            var socket = new io.Socket();
            socket.connect();
            socket.on('connect', function() {
                socket.subscribe('my channel');
                socket.send('asdf');
            });
        </script>
   </object>
  </head>

  <body>

    <div>

    </div>
  </body>
</html>

Now when I run the webiste, after I visit it in a browser, I get the following on my terminal: 现在,当我运行Webiste时,在浏览器中对其进行访问后,在终端机上显示以下内容:

IP - - [DATE] "GET /socketio_scripts.html HTTP/1.1" 404 135 0.004273

Why does it not find the other html file? 为什么找不到其他HTML文件?

I suspect that /socketio_scripts.html is not the correct url for that file - it seems odd that that file would be at the root of your document tree. 我怀疑/socketio_scripts.html是不正确url该文件-这似乎很奇怪,该文件会在文档树的根。

According to several pages, it should be in .../templates/ . 根据几页,它应该位于.../templates/ I also doubt very much it would ever be accessed by GET 我也非常怀疑GET是否会访问它

Have a look at the web.py tutorial . 看一下web.py教程 One big difference with your code is that all the pages have to be listed in the urls dispatch table: 您的代码的一大不同之处在于,所有页面都必须在urls发表中列出:

urls = (
  '/', 'hello',
  '/bye', 'bye')

The static files which you dont want to be served from GET function have to be placed in a static file , in your example the "socketio_scripts.html" and accessed by the full url " http://localhost/static/socketio_scripts.html ". 您不想从GET函数提供的静态文件必须放置在静态文件中,在您的示例中为“ socketio_scripts.html”,并通过完整的URL“ http://localhost/static/socketio_scripts.html ”进行访问。 http://webpy.org/cookbook/staticfiles http://webpy.org/cookbook/staticfiles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM