0

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
ZCJ
  • 499
  • 2
  • 9
  • 17
  • possible duplicate of [How to run python scripts on a web server(e.g localhost)](http://stackoverflow.com/questions/20242434/how-to-run-python-scripts-on-a-web-servere-g-localhost) – T.Todua Jul 16 '14 at 08:01

2 Answers2

4

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)

avasal
  • 14,350
  • 4
  • 31
  • 47
  • This still doesn't seem to work. I'll try some more things to fix the error I must've created... Is there any chance my script isn't saved in the right place? Could that affect how/if it's loaded? – ZCJ Feb 17 '12 at 05:06
  • 1
    no that doesn't seems to be the problem, i used ur code of app.py and it executed fine for me. Try reinstalling webpy is all i can say for now.. – avasal Feb 17 '12 at 05:20
0

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
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Prajapathy3165
  • 518
  • 1
  • 6
  • 13