-1

I have to following flask (python) server:

from flask import Flask, render_template, request, redirect, Response,jsonify
import random, json

app = Flask(__name__)


@app.route('/')
def output():
    return redirect("This is a temporary implementation.")

@app.route('/model',methods = ['POST'])
def ask_model():
    data = request.get_json(force=True)
    print("data recieved: "+str(data))
    return jsonify({'vlaue': "some response"})



if __name__ == '__main__':
    # run!
    #the "IP:Port" the server listens to is "localhost:5000"
    app.run()

I also have a JS script running on a website called slither.io (source: https://github.com/ErmiyaEskandary/Slither.io-bot)

I want to create a learning bot and in order to do that I need to send requests between this JS script and the server, but I don't how to do it.

for example, I added to following lines to the script:

console.log("pre-post");
$.post('http://localhost:5000/model',JSON.stringify(canvasUtil.mapToMouse(window.goalCoordinates)),
     function(){
         console.log("entered model function.");
                },'json');
console.log("post-post");

When I look at the console log only "pre-post" is printed, and my server doesn't receives any request.

Why doesn't my requests get to my local server? How do i fix this so my server will get the requests and respond to them?

Yair
  • 85
  • 1
  • 9
  • Are you testing this code on the same machine that your local server runs on? Otherwise this obviously can’t work. – CBroe Jul 28 '17 at 10:47
  • And even if you test this locally, if the site the script runs on was requested using any other origin than `http://localhost:5000/`, this would still be a cross-domain request, so your app would first of all have to handle the CORS pre-flight request correctly ... – CBroe Jul 28 '17 at 10:49
  • Thank you for your response. can you explain a little bit further or give some source to read? (1) the server runs on my local computer and the JS script I run using greaseMonkey, which as far as i understood just calling the script from the console. why shouldn't it work? (2) what is a cross-domain request and why is relevant to my case? I just want to send a request from the script to my server and respond to it – Yair Jul 28 '17 at 10:54
  • _“what is a cross-domain request”_ is something you can type into Google, please. – CBroe Jul 28 '17 at 10:57

1 Answers1

1

As CBore said, you have to handle the COORS correctly, then you have to use the IP addres of your localhost server, but you should have a static IP or when your localhost ip address changes the request doesn't reach your server anymore

Sim1-81
  • 618
  • 4
  • 14
  • _“As CBore said, you have to handle the COORS correctly”_ – well I did not actually say that they should drink cheap beer ;) – CBroe Jul 28 '17 at 10:58
  • Thank you for your response. Is COORS and CORS are the same things, i.e will those sources help me solve my problem?: (1) https://www.html5rocks.com/en/tutorials/cors/ (2) https://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Yair Jul 28 '17 at 11:03