为什么Web.py不让我在80端口运行服务器?

6 投票
5 回答
17958 浏览
提问于 2025-04-17 06:15

我正在尝试用Web.py创建一个网站,但它不让我在80端口上打开一个连接,其他端口都可以用。

我已经进行了端口转发,所以这不是问题所在。

python main.py 80

但是当我这样做时,我收到了这个错误:

http://0.0.0.0:80/
Traceback (most recent call last):
  File "main.py", line 43, in <module>
    app.run()
  File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 311, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "/usr/local/lib/python2.7/dist-packages/web/wsgi.py", line 54, in runwsgi
    return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
  File "/usr/local/lib/python2.7/dist-packages/web/httpserver.py", line 148, in runsimple
    server.start()
  File "/usr/local/lib/python2.7/dist-packages/web/wsgiserver/__init__.py", line 1753, in start
    raise socket.error(msg)
socket.error: No socket could be created

到目前为止我的代码是:

import MySQLdb
import web
import hashlib as h


urls = (

'/', "index", "/register/?", "register", "/login/?", "login", "/thankyou/?", "thankyou"

)

app = web.application(urls, globals())
render = web.template.render("templates/")
db = web.database (dbn="mysql", user="root", pw="461408", db="realmd")

class index():
    def GET(self):
        return render.index()
class register():
    def GET(self):
        return render.register()
    def POST(self):
        i = web.input()
        user = h.sha1(i.username).hexdigest()
        pw = h.sha1(i.password).hexdigest()

        n = db.insert("account", username=user, password=pw)




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

有人能帮帮我吗?

5 个回答

4

在浏览器里输入 127.0.0.1。这个地址通常是指你自己电脑上的一个地方。很可能已经有一个程序在使用80号端口,而这个端口是用来处理网页请求的,所以这样查看一下,最简单的方法就是看看是什么程序在用这个端口。

6

我成功地通过这个命令在80端口启动了服务。

sudo python index.py 80

但是当我按快捷键(control+c)来关闭服务时,会出现一个错误。

  ^CTraceback (most recent call last):
  File "application.py", line 206, in <module>
    app.run()
  File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/application.py", line 313, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgi.py", line 54, in runwsgi
    return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))
  File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 159, in runsimple
    server.stop()
  File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1932, in stop
    self.requests.stop(self.shutdown_timeout)
  File "/Library/Python/2.7/site-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1471, in stop
    worker.join(remaining_time)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 680, in join
    self.__block.release()
thread.error: release unlocked lock
^C^CException KeyboardInterrupt in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored

出现这种情况时,我会结束所有的Python进程...

killall -9 python

这样可以解决上面的问题,但不太推荐这样做。

12

你可能有其他程序正在使用80号端口。你可以试试这个命令 netstat -ln | grep 80 来检查一下。

另外,你也可以试试 telnet localhost 80,如果连接被拒绝,那说明这个端口可以用。

撰写回答