Posting a move from Python

#1

I have tried different ways to be able to post a move from python but without success so far.

Using the requests package I can use most of the REST API functionality but “games/id/move” returns 500 server error.

def make_move(token,game_id,move):
    url = 'https://online-go.com/api/v1/games/{}/move/?%s'.format(game_id,urlencode({"move":move}))
    ogs_response = requests.post(url, headers={"Authorization" : "Bearer " + token})
    return ogs_response.text

Calling this function returns

u'<h1>Server Error (500)</h1>'

Using the websocket package to connect to the real time API doesn’t work either but I cannot even see the error messages.

def on_message(ws, message):
    print "Message"

def on_error(ws, error):
    print("ERROR: ", error)

def on_close(ws):
    print("Goodbye!")
    
def on_data(ws,data,_,_2):
    print "Data"

def on_open(ws):
    print "Opening"
    print ws.send("net/ping")
    print ws.send(to_message("authenticate",data={"auth":token}))
    print ws.send(to_message("game/connect",data={'game_id': game_id, 'player_id': user_id, 'chat': 1, 'game_type': "game", 'auth':g_token}))
    print ws.send(to_message("game/move",data={"auth":g_token,"game_id":game_id,"player_id":user_id,"move":"hn"}))
    ws.send("game/move?%s"%urlencode({"auth":token,"game_id":game_id,"player_id":user_id,"move":"cq"}))

websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://online-go.com/socket.io/?transport=websocket",
                          on_message = on_message,
                          on_error = on_error,
                          on_close = on_close,
                          on_data = on_data)

ws.on_open = on_open
ws.run_forever()

This outputs:

--- request header ---
GET /socket.io/?transport=websocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: online-go.com
Origin: http://online-go.com
Sec-WebSocket-Key: EapZhb/Lbe6EluYF3lnoAg==
Sec-WebSocket-Version: 13
Cookie: __cfduid=d4455a122c042375949595b903a9f330c1503505931


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Date: Wed, 23 Aug 2017 17:49:01 GMT
Connection: upgrade
encoding
Upgrade: websocket
Sec-WebSocket-Accept: CCd+dj2lqTWtWgprY4J1tJJGBWY=
Sec-WebSocket-Version: 13
WebSocket-Server: uWebSockets
Server: cloudflare-nginx
CF-RAY: 392fe7f218f96b61-LHR
-----------------------
send: '\x81\x88\x061jYhT\x1evvX\x04>'
send: '\x81\xbd\xea-\x00\xde\xde\x1f[\xfc\x8bXt\xb6\x8fCt\xb7\x89Lt\xbb\xc8\x01{\xfc\x8bXt\xb6\xc8\x17 \xfc\x80Hg\xe9\x87{O\x9f\x80gQ\x8d\x87is\x93\xadiO\x98\xae|8\x92\x85iU\xe9\x9aK"\xa3\xb7'
send: '\x81\xfe\x00\x88\xb5\xa9b\xb9\x81\x9b9\x9b\xd2\xc8\x0f\xdc\x9a\xca\r\xd7\xdb\xcc\x01\xcd\x97\x85\x19\x9b\xc5\xc5\x03\xc0\xd0\xdb=\xd0\xd1\x8bX\x99\x81\x9fT\x88\x84\x90N\x99\x97\xce\x03\xd4\xd0\xf6\x0b\xdd\x97\x93B\x80\x82\x9eQ\x8e\x86\x99N\x99\x97\xce\x03\xd4\xd0\xf6\x16\xc0\xc5\xcc@\x83\x95\x8b\x05\xd8\xd8\xcc@\x95\x95\x8b\x03\xcc\xc1\xc1@\x83\x95\x8bQ\x8f\xd4\xcf\x06\xda\xd4\x9c\x03\x89\x80\xcfW\x89\x8c\x90Q\xdf\x84\x9fP\xdc\xd0\xcbQ\xdc\x83\x9b\x01\x8e\xd6\x9f@\x95\x95\x8b\x01\xd1\xd4\xdd@\x83\x95\x98\x1f\xe4'
send: "\x81\xf3\\\xb9|\x8fh\x8b'\xad;\xd8\x11\xeas\xd4\x13\xf99\x9bP\xf4~\xc9\x10\xee%\xdc\x0e\xd05\xdd^\xb5|\x8dJ\xb9m\x88E\xa3|\x9b\x1b\xee1\xdc#\xe68\x9bF\xafe\x8eK\xbck\x8aL\xa3|\x9b\x11\xe0*\xdc^\xb5|\x9b\x14\xe1~\x95\\\xad=\xcc\x08\xe7~\x83\\\xado\x8f\x1d\xe98\xda\x1d\xba=\x89I\xe9i\x89E\xb6o\xdfM\xb9n\xdc\x19\xedo\xdcJ\xbd?\x8e\x1f\xb9~\xc4!"
send: '\x81\xd6U}\xb742\x1c\xdaQz\x10\xd8B0B\xc7X4\x04\xd2F\n\x14\xd3\taK\x81\x05dD\x91S4\x10\xd2k<\x19\x8a\rbJ\x84\x03fM\x91Y:\x0b\xd2\t6\x0c\x91U \t\xdf\t?\x18\xd0\x038+\xf8u?7\xe6g89\xc4y\x129\xf8r\x11,\x8fx:9\xe2\x03%\x1b'
send: '\x88\x82\x9b\x8f\x87<\x98g'
Opening
None
None
None
None
Data
Message
Data
Message
Data
Message
Goodbye!

Does anyone have a solution?

0 Likes

#2

Unfortunately I don’t have a solution, in fact I can confirm you I have the same problem (error 500), using REST API via a Net.WebRequest of Visual Basic 2015.

0 Likes

#3

Have you tried using the python socket io client library instead of the websocket library? Socket io is a layer on top of websockets.

With that said, my “authenticate” call in my client has more info than yours:

        socket!!.emit("authenticate", createJsonObject {
            put("player_id", uiConfig.user.id)
            put("username", uiConfig.user.username)
            put("auth", uiConfig.chat_auth)
        })

I’m not sure if all those fields are necessary. When I was creating my client I mostly looked at the network trace and did what the web site did.

0 Likes

#4

Hi @nathanj439 thanks for your help. I have tried socketio-client but the problem is that there is no way to use transport = websocket for the first message, it uses xhr-polling and then upgrades to websocket, which is not supported by the server.

I’ll try adding this information and see what happens, but my main problem is that I don’t understand what websocket does because of these messages:

send: '\x81\x88\x061jYhT\x1evvX\x04>'

I think there is some kind of a double encoding problem that I haven’t been able to solve so far.

I do not know how to look at the network trace, I will research that as that seems very useful.

0 Likes