There is a very simple CGI server on Python. He is sent a request by the client (JS):

var json = JSON.stringigy({foo: "bar", baz: [0, 1, 2]}); var xhr = new XmlHttpRequest(); xhr.open("POST", "/cgi-bin/script.py", true); xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); xhr.send(json); 

script.py should get this JSON, parse and, preferably, send to the client (by changing, of course). I apologize in advance for the Nuba question, I have broken Google all day, have not found anything.

  • And the question is what? - Vladimir Martyanov
  • what did you try to do, what didn't work out? server listens, accepts, processes, sends back, what's the problem? - approximatenumber
  • I do not know how to implement the server. - EvaBob
  • Did not find information on how to handle "application / json" on Python. - EvaBob
  • @EvaBob look better. Here is an example of a simple http server from the first search results. By the way, you wrote first that " ... There is a very simple CGI server ... ". And say, not available. - approximatenumber

1 answer 1

Here is a working example:

script.py

 # -*- coding: utf-8 -*- import sys import json myjson = json.loads(sys.stdin.read(int(os.environ['CONTENT_LENGTH']))) # здесь мы что-то меняем в данных myjson['baz'] = range(100) # возвращаем данные print 'Content-Type: application/json' print 'Content-Length: ', len(json.dumps(myjson)) print 'Connection: keep-alive' print 'Content-Language: ru' print print json.dumps(myjson) # or "json.dump(result, sys.stdout)" 

and you have a couple of errors in the JS code, here is my example:

 var json = JSON.stringify({foo: "bar", baz: [0, 1, 2]}); var xhr = new XMLHttpRequest(); xhr.open("POST", "/cgi-bin/script.py", true); xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); xhr.send(json); xhr.onreadystatechange = function(){ var json = xhr.response; console.log(json); } 

taken from here