我是新來的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()