1

Here is the code that I have tried:

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()

The error I got is:

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

So I try to remove the __file__ from the command:

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()

Go the following error:

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

Kindly, help me in understanding the missing part as I guess I missed something.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 1
    its -> `{'path': os.path.dirname('__file__')}` – Kunal Mukherjee Nov 27 '18 at 13:12
  • 1
    @KunalMukherjee Thank you. I thought I missed something else. It helped me. – Jaffer Wilson Nov 27 '18 at 13:26
  • 1
    No. Just using `'__file__'` as a string is not a real solution. This will pass an empty string as the `path` argument to the StaticFileHandler constructor (Because `os.path.dirname` returns the _head_ part of the pair returned by `os.path.split`, and that will be empty if the input string does not contain a slash). `__file__` is not set in your case because you run your code interactively. I recommend to manually set `{'path': ''}` to a folder that actually exists on your machine to serve the static files from. – shmee Nov 27 '18 at 13:54

0 Answers0