2011-02-28 56 views
0

我是新來的python WSGI &我試圖建立我自己的服務器來測試我的login.html頁面(使用AJAX)。自定義WSGI服務器連接失敗

但是當我去跑我WSGI.py(我的服務器我通過下面的教程製作)我得到這個錯誤:

Traceback (most recent call last):
File "C:\Users\Print\Desktop\Website\KazCare\wsgi.py", line 63, in start_server()
File "C:\Users\Print\Desktop\Website\KazCare\wsgi.py", line 57, in start_server httpd = make_server("", PORT, test_app)
File "C:\Python27\lib\wsgiref\simple_server.py", line 144, in make_server server = server_class((host, port), handler_class)
File "C:\Python27\lib\SocketServer.py", line 408, in init self.server_bind()
File "C:\Python27\lib\wsgiref\simple_server.py", line 48, in server_bind HTTPServer.server_bind(self)
File "C:\Python27\lib\BaseHTTPServer.py", line 108, in server_bind SocketServer.TCPServer.server_bind(self)
File "C:\Python27\lib\SocketServer.py", line 419, in server_bind self.socket.bind(self.server_address)
File "C:\Python27\lib\socket.py", line 224, in meth return getattr(self._sock,name)(*args)
error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

你覺得我做錯了嗎?

這裏是我的服務器:

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() 

回答

1

我不知道你在做什麼錯事。它聽起來像你沒有權限綁定到該端口。我知道這可能是* nix中的一個問題,如果要綁定到端口80,則必須以sudo身份運行。我不假裝知道它如何在Windows中工作。嘗試更改爲通常不與HTTP(8888或9999)關聯的端口,然後查看是否有效。您也可以嘗試以管理員身份運行。

1

它可能已被別的東西使用。嘗試從您的網絡瀏覽器訪問它作爲http://localhost:8080/,看看是否有任何反應。

相關問題