自定义WSGI服务器连接失败

时间:2011-02-28 04:39:21

标签: python cgi wsgi

我是python WSGI&的新手。我正在尝试设置自己的服务器来测试我的login.html页面(使用AJAX)。

但是当我去运行我的WSGI.py(我通过遵循教程制作的服务器)时,我收到此错误:

  

追踪(最近的呼叫最后):
    文件“C:\ Users \ Print \ Desktop \ Website \ KazCare \ wsgi.py”,第63行,in       START_SERVER()
    在start_server中的文件“C:\ Users \ Print \ Desktop \ Website \ KazCare \ wsgi.py”,第57行       httpd = make_server(“”,PORT,test_app)
    make_server中的文件“C:\ Python27 \ lib \ wsgiref \ simple_server.py”,第144行       server = server_class((host,port),handler_class)
    文件“C:\ Python27 \ lib \ SocketServer.py”,第408行,在 init 中       self.server_bind()
    在server_bind中的文件“C:\ Python27 \ lib \ wsgiref \ simple_server.py”,第48行       HTTPServer.server_bind(个体经营)
    在server_bind中的文件“C:\ Python27 \ lib \ BaseHTTPServer.py”,第108行       SocketServer.TCPServer.server_bind(个体经营)
    在server_bind中的文件“C:\ Python27 \ lib \ SocketServer.py”,第419行       self.socket.bind(self.server_address)
    文件“C:\ Python27 \ lib \ socket.py”,第224行,在meth中       return getattr(self._sock,name)(* args)
  错误:[Errno 10013]尝试以其访问权限禁止的方式访问套接字

你认为我做错了什么?

这是我的服务器:

import threading
import webbrowser
from wsgiref.simple_server import make_server

FILE = 'frontend.html'
PORT = 8080

def test_app(environ, start_response):

    if environ['REQUEST_METHOD'] == 'POST':

        try:
            request_body_size = int(environ['CONTENT_LENGTH'])
            request_body = environ['wsgi.input'].read(request_body_size)
        except (TypeError, ValueError):
            request_body = "0"

        try:
            response_body = str(int(request_body) ** 2)
        except:
            response_body = "error"

        status = '200 OK'
        headers = [('Content-type', 'text/plain')]
        start_response(status, headers)
        return [response_body]

    else:
        response_body = open(FILE).read()
        status = '200 OK'
        headers = [('Content-type', 'text/html'), ('Content-Length', str(len(response_body)))]
        start_response(status, headers)
        return [response_body]

def open_browser():
    """Start a browser after waiting for half a second."""

    def _open_browser():
        webbrowser.open('http://localhost:%s/%s' % (PORT, FILE))
        thread = threading.Timer(0.5, _open_browser)
        thread.start()

def start_server():
    """Start the server."""
    httpd = make_server("", PORT, test_app)
    httpd.serve_forever()


if __name__ == "__main__":
    open_browser()
    start_server()

2 个答案:

答案 0 :(得分:1)

我认为你做错了什么。听起来你没有绑定到该端口的权限。我知道这可能是* nix中的一个问题,如果你想绑定到端口80,你必须以sudo身份运行。我不假装知道它在Windows中是如何工作的。尝试更改为通常与HTTP无关的端口(8888或9999),看看是否有效。您也可以尝试以管理员身份运行。

答案 1 :(得分:1)

它可能已经被其他东西使用了。尝试从您的网络浏览器访问http://localhost:8080/并查看是否有任何响应。

相关问题