0
votes

I am new to python WSGI & I am attempting to set up my own server to test my login.html page (that uses AJAX).

But when I go to run my WSGI.py (my server I made through following a tutorial) I get this error:

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

What do you think I am doing wrong?

Here is my server:

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

2 Answers

1
votes

I don't think you are doing anything wrong. Its sounds like you don't have permissions to bind to that port. I know this can be an issue in *nix, and you have to run as sudo if you want to bind to port 80 for instance. I don't pretend to know how it works in windows though. Try changing to a port not generally associated with HTTP (8888 or 9999), and see if that works. You could also try and run the as an administrator.

1
votes

It may already be in use by something else. Try accessing it from your web browser as http://localhost:8080/ and see if anything responds.