【问题标题】:Why wont Web.py let me run a server on port 80?为什么 Web.py 不允许我在 80 端口上运行服务器?
【发布时间】:2026-01-06 05:30:01
【问题描述】:

我正在尝试使用 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()

有人可以帮忙吗?

【问题讨论】:

  • 您是否有其他服务正在侦听端口 80?喜欢阿帕奇?
  • "...它适用于所有其他端口。"真的吗?
  • 你试过81端口吗?可能是权限错误。在 Linux 上(至少),你需要是 root 才能在这样的低端口上监听。 (我认为)。
  • 您使用的是什么操作系统?您还尝试了哪些其他端口?
  • 所以你试过 65534 其他端口? ;)

标签: python web.py


【解决方案1】:

可能是因为您试图以非特权用户身份启动 web.py 吗?

尝试:

sudo python ./bin/blah.py

【讨论】:

    【解决方案2】:

    我在 RaspberryPi 上遇到了同样的问题。为了解决这个问题,我刚刚在命令之前添加了 sudo。试试:sudo python main.py 80

    【讨论】:

    • 欢迎来到这个网站,艾迪。您的回答重复了上面已经写过的关于权限的内容,但是......您提供的 sudo 语法大致意思是“像 root 一样运行”。
    【解决方案3】:

    您可能在端口 80 上有其他工作。请尝试使用命令 netstat -ln | grep 80 进行检查。

    或者,您可以尝试telnet localhost 80,如果连接被拒绝,那么该端口应该可以使用。

    【讨论】:

      【解决方案4】:

      我在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
      

      可以解决以上问题,但不推荐

      【讨论】:

        【解决方案5】:

        在浏览器中访问127.0.0.1。可能已经有一个进程使用端口 80,并且该端口应该用于 http,因此这可能是查看正在使用它的最简单的方法。

        【讨论】: