0

I'm trying to make a javascript program send data to a python socket but it doesn't receive the right data.

I want python to print 'aaaa'.

Here is my javascript code:

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    xhr = null;

  }
  return xhr;
}



var xhr = createCORSRequest('GET', "http://192.168.1.10:12345");


xhr.send("aaaa");

Here is my python code:

import socket

s = socket.socket()
host = socket.gethostname()
port = 12345
BUFFER_SIZE = 1024
s.bind(('', port))

s.listen(5)
while True:
    c, addr = s.accept()
    print ('Got connection from', addr)
    c.send(bytes('Thank you for connecting','UTF-8'))
    data = c.recv(BUFFER_SIZE)
    print(data)
    c.close()
2
  • Are you sending the javascript through your browser? Commented May 22, 2016 at 18:42
  • yes i am sending the javascript through my browser Commented May 23, 2016 at 10:53

2 Answers 2

1
  1. You are doing an XMLHttpRequest, which is a HTTP request. But your python server does not handle the HTTP protocol at all. Handling HTTP would mean to read the HTTP request header, read the body based on the information in the header and return a proper HTTP response.
  2. You are doing a HTTP GET requests. A GET requests takes no payload so any body data you add (i.e. the "aaaa" in your xhr.send("aaaa")) will be ignored (means: not send). To send a HTTP body use request types like POST.
Sign up to request clarification or add additional context in comments.

Comments

1

Steffen's answer is correct (at least in general - can't comment on the JS specifics). In addition, it's always a good idea to independently verify the moving parts of your application, so that you can narrow down where the problem is.

Here's how you can verify that your python server works from the command line:

  1. Start the server
  2. In another terminal window, connect to it using telnet

    telnet localhost 12345

    (It will first try connecting using IPv6, fail, and fall back to IPv4)

  3. You will see your welcome message returned to the client. Enter some text and press Enter.

  4. The server will print your message and close the connection to the client.

Using your code, here's how it will look for the client. I'm sending the text meow to the server:

margold@home-macbook ~ $ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingmeow
Connection closed by foreign host.

And for the server:

margold@home-macbook ~ $ ./server.py
('Got connection from', ('127.0.0.1', 61148))
meow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.