ZCJ
ZCJ

Reputation: 499

Running python app on localhost

I'm trying to get a simple python script from the learnpythonthehardway tutorial to show up on my browser but keep encountering getting the following error:

$ python app.py
http://0.0.0.0:8080/
Traceback (most recent call last):
File "app.py", line 15, in <module>
app.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/application.py", line 310, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/wsgi.py", line 54, in runwsgi
return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/httpserver.py", line 148, in runsimple
server.start()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/wsgiserver/__init__.py", line 1753, in start
raise socket.error(msg)
socket.error: No socket could be created

The script app.py is this:

import web

urls = (
  '/', 'index'
)

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

class index:
    def GET(self):
        greeting = "Hello World"
        return greeting

if __name__ == "__main__":
    app.run()

What should I try now? It actually worked to print Hello World on my browser once, but I've been meddling with it and now it's giving me error messages regardless of what I try. I don't know what these messages mean or how to fix them.

Upvotes: 0

Views: 5830

Answers (2)

Prajapathy3165
Prajapathy3165

Reputation: 586

Changing the port address could fix this. Probably 8080 might be used for something else. Idk im speculating. The following fixed the issue for me.

try

$ python app.py 8081

Upvotes: 0

avasal
avasal

Reputation: 14854

The problem is due to the way web.py loading works, you need to have the creation and running of the app only occur if this is the main entry point of the program.

Otherwise, the class loader in web.py will reload this file again later, and wind up spawning a new server when its intent was simply to load a class.

try this

if __name__ == '__main__' :
  app = web.application(urls, globals())
  app.run()

Source: No socket could be created (other causes)

Upvotes: 4

Related Questions