Вопрос

I am into implementing a pull communication between my javascript client and python server. I have wasted my last two weeks struggling with websockets and socketing, but now I decided to give it up.

I am looking for the most convenient way to implement python-javascript communication where the client sends requests to the server, to which the server can answer. I have seen lots of ways, some of which:

  • ajax
  • json
  • socket, etc

I would love to hear your opinion on the advantages of the different possibilities. ( If you also have some great tutorials which I can use to get started, I would really appreciate them. )

Это было полезно?

Решение

It seems you are a little bit confused on definitions and technologies, so let me start of by clarifying some defitintions (rough descriptions, not exact):

  • JSON: A format to pack data structures (like arrays) into a string and to retrieve the data. It is one the most used formats to submit actual data but is by itself not capable of transmitting data: It is only the data itself
  • AJAX: The dominant technology for sending requests to a server without reloading the complete page. AJAX works through the HTTP protocol and sends the same headers as would be sent if you would send the same request by visiting the page (Exception: Headers that are special to AJAX, depends on library etc.). By itself AJAX is the technology to submit the data but is not the data itself. However, it can, for example, submit form data encoded in the usual URL encoding. But it can also send JSON data
  • Sockets: The definition of a socket is very broad and general. Normally, it describes a low layer like TCP (below HTTP in hierarchy) but since you are describing JS + Python I would assume you mean browser - client communication. In that case there is WebSocket. I have not worked with that technology yet but from what I hear it allows arbitrary connections etc. For a normal use case way to advanced.

These are the technologies you mention. Additionally, I would like to mention two more:

  • XML: Like JSON, but a different format. XML has various advantages and disadvantages. From my point of view the biggest advantage is that it more flexible and on complex structures easier to understand than JSON. But this flexibility comes with the disadvantage that it is harder to learn.
  • JSONP: Similar to AJAX in that it is used for communication but as the name states, it submits JSON data (I think...). Has the advantage of allowing cross-domain requests (also advanced stuff).

Since your question is not that precise, I will answer it (like assumed above) in the Web context: JavaScript is run from the browser and the server is a web application written in Python.

For this I would recommend a combination of JSON & AJAX: JSON is natively available in both JavaScript and Python and libraries like jQuery offer easy interfaces to data recieved via JSON. I recommend AJAX for the same reason: It's easy to use, integrated in most JavaScript libraries and needs no work on the server side whatsoever: For the server there is just a request to handle.

Example on the server side:

# Request comes in:
data = json.loads(request_body)
# Do calculations
output = json.dumps(out_data)
send_response(output)

On the client side, you could plain JavaScript or jQuery for sending the request and handling the response.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top