Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python HTTP server not responding on POST request

I would like to make a HTTP server using python, to host my website. I've implemented do_GET() method and it works fine, but when I try the POST method, server is not responding until I reload the page. Then it wakes up, tring to complete the POST, but realizing that the connection has ended. After that it serves the GET method (because I reloaded the page) and continue running normally.

I'm using jQuery on client side, but i also tried html form. It's no change even if i open the page in another browser, so it must be an exception on server side.

Here is the client javascript:

function Send()
        {
            jQuery.post("address", "Blah!", function (data) {
                //get response
                alert(data);
            });
        }

And here is the server code (I'm using python 3.3.4):

class MyHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        print("Data: " + str(self.rfile.read(), "utf-8")) #show request

        response = bytes("This is the response.", "utf-8") #create response

        self.send_response(200) #create header
        self.send_header("Content-Length", str(len(response)))
        self.end_headers()

        self.wfile.write(response) #send response

This is the log from server:

10.175.72.200 - - [01/Apr/2014 19:11:26] "GET / HTTP/1.1" 200 -
Data: Blah!
10.175.72.200 - - [01/Apr/2014 19:11:47] "POST /address HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('10.175.72.200', 2237)
Traceback (most recent call last):
  File "C:\Python33\lib\socketserver.py", line 306, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Python33\lib\socketserver.py", line 332, in process_request
    self.finish_request(request, client_address)
  File "C:\Python33\lib\socketserver.py", line 345, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Python33\lib\socketserver.py", line 666, in __init__
    self.handle()
  File "C:\Python33\lib\http\server.py", line 400, in handle
    self.handle_one_request()
  File "C:\Python33\lib\http\server.py", line 388, in handle_one_request
    method()
  File "C:\Users\Jirek\Desktop\Server\server.py", line 45, in do_POST
    self.wfile.write(response) #send response
  File "C:\Python33\lib\socket.py", line 317, in write
    return self._sock.send(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
10.175.72.200 - - [01/Apr/2014 19:11:47] "GET / HTTP/1.1" 200 -

You can see, on the first line is classical GET request. Then I call the POST from browser and nothing happens until the reload. At that point the server writes the rest of the log.

like image 239
Jirka Mayer Avatar asked Apr 01 '14 18:04

Jirka Mayer


1 Answers

Problem solved! It's in the line where the code loads received data (in do_POST()). I just had to specify the amount of data to read.

So, just don't use rfile.read(), but rfile.read(numberOfBytes). How big is the POST message is saved in headers: numberOfBytes = int(self.headers["Content-Length"])

The repaired method:

def do_POST(self):
    length = int(self.headers["Content-Length"])
    print("Data: " + str(self.rfile.read(length), "utf-8"))

    response = bytes("This is the response.", "utf-8") #create response

    self.send_response(200) #create header
    self.send_header("Content-Length", str(len(response)))
    self.end_headers()

    self.wfile.write(response) #send response
like image 138
Jirka Mayer Avatar answered Nov 16 '22 16:11

Jirka Mayer