0 votes

I am using the following code to attempt to connect to a local server I have set up using SimpleHTTPServer. However, every time i get the error message saying the the assertion has failed. I have tried numerous different url's in the connect function but nothing is working. I am unfamiliar with connecting to servers so any help would be greatly appreciated.

 func talkToServer(url, mode, data):
    # Connect to host/port
    HTTP = HTTPClient.new()
    RESPONSE = HTTP.connect("http://localhost:8000/", 8000)
    # Wait until resolved and connected
    while HTTP.get_status() == HTTPClient.STATUS_CONNECTING or HTTP.get_status() == HTTPClient.STATUS_RESOLVING:
        HTTP.poll()
        OS.delay_msec(300)
    # Error catch: Could not connect
    assert(HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
    # Check for a GET or POST command
    if data == "":
        HEADERS =["User-Agent: Pirulo/1.0 (Godot)", "Accept: */*"]
        RESPONSE = HTTP.request(HTTPClient.METHOD_GET, url, HEADERS)
    else:
        QUERY = HTTP.query_string_from_dict(data)
        HEADERS = ["User-Agent: Pirulo/1.0 (Godot)", "Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(QUERY.length())]
        RESPONSE = HTTP.request(HTTPClient.METHOD_POST, url, HEADERS, QUERY)
    # Make sure all is OK
    assert(RESPONSE == OK)
    # Keep polling until the request is going on
    while (HTTP.get_status() == HTTPClient.STATUS_REQUESTING):
        HTTP.poll()
        OS.delay_msec(300)
    # Make sure request finished
    assert(HTTP.get_status() == HTTPClient.STATUS_BODY or HTTP.get_status() == HTTPClient.STATUS_CONNECTED)
    # Set up some variables
    var RB = RawArray()
    var CHUNK = 0
    var RESULT = 0
    # Raw data array
    if HTTP.has_response():
        # Get response headers
        var headers = HTTP.get_response_headers_as_dictionary()
        while HTTP.get_status() == HTTPClient.STATUS_BODY:
            HTTP.poll()
            CHUNK = HTTP.read_response_body_chunk()
            if(CHUNK.size() == 0):
                OS.delay_usec(100)
            else:
                RB = RB + CHUNK
            HTTP.close()
            RESULT = RB.get_string_from_ascii()

I am getting the following error message in my terminal:

127.0.0.1 - - [03/Oct/2017 17:01:57] code 501, message Unsupported method ('POST')
127.0.0.1 - - [03/Oct/2017 17:01:57] "POST blankJSON.json HTTP/1.1" 501 -
asked Oct 3, 2017 in Engine by stubbsy345 (282 points)
edited Oct 3, 2017 by stubbsy345

1 Answer

0 votes
Best answer

This is not a problem with Godot, but the way you're using Python's SimpleHTTPServer module.

The example code in the documentation does not support POST requests, but if you extend it with a custom handler class, you can get something that works for debugging purposes:

import SimpleHTTPServer
import SocketServer
import json

PORT = 8000

class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_POST(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

        self.wfile.write(json.dumps({"success": True}))

        content_length = int(self.headers['Content-Length'])
        json_string = self.rfile.read(content_length)
        data = json.loads(json_string)
        print data

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

Note that the key here is to provide a handler which implements the do_POST() method.

You can test this back-end with a simple curl request:

curl -d '{"foo":"bar","baz":true}' http://localhost:8000

This is fine for the purpose of debugging, but if you plan on implementing a real back-end in Python, I would suggest using Flask instead, which comes with lots of nice features out of the box.

answered Oct 3, 2017 by flesk (134 points)
selected Oct 3, 2017 by stubbsy345

Thanks bro this is great explanation!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.