龙卷风套接字服务器没有开始使用Python

时间:2018-11-27 12:49:15

标签: python python-3.x websocket tornado

这是我尝试过的代码:

if __name__ == "__main__":
    #create new web app w/ websocket endpoint available at /websocket
    print ("Starting websocket server program. Awaiting client requests to open websocket ...")
    application = web.Application([(r'/static/(.*)', web.StaticFileHandler, {'path': os.path.dirname(__file__)}),
                                   (r'/websocket', WebSocketHandler)])
    application.listen(8001)
    ioloop.IOLoop.instance().start()

我得到的错误是:

NameError                                 Traceback (most recent call last)
<ipython-input-41-cdf099b27f7d> in <module>()
      2     #create new web app w/ websocket endpoint available at /websocket
      3     print ("Starting websocket server program. Awaiting client requests to open websocket ...")
----> 4     application = web.Application([(r'/static/(.*)', web.StaticFileHandler, {'path': os.path.dirname(__file__)}),
      5                                    (r'/websocket', WebSocketHandler)])
      6     application.listen(8001)

NameError: name '__file__' is not defined

因此,我尝试从命令中删除__file__

if __name__ == "__main__":
    #create new web app w/ websocket endpoint available at /websocket
    print ("Starting websocket server program. Awaiting client requests to open websocket ...")
    application = web.Application([(r'/'),
                                   (r'/websocket', WebSocketHandler)])
    application.listen(8001)
    ioloop.IOLoop.instance().start()

执行以下错误:

AttributeError: 'str' object has no attribute 'name'

请帮助我理解缺失的部分,因为我猜我错过了一些东西。

0 个答案:

没有答案