简体   繁体   English

Python JSON-RPC_2.0 TCP服务器客户端说明

[英]Python JSON-RPC_2.0 TCP Server Client Explained

I'm having a difficult time fully understanding the nature of a TCP server/client relationship when a JSON string is sent to the server. 当将JSON字符串发送到服务器时,我很难理解TCP服务器/客户端关系的性质。 The information I need may be out there, but I'm perhpas not using the correct search paramaters as I'm looking. 我需要的信息可能在那儿,但是我一直在寻找正确的搜索参数。

I've built a Python TCP, JSON-RPC Server from the following examples: 我已经从以下示例构建了Python TCP,JSON-RPC Server:

In both cases, I can communicate with the Python server from a Python console on a different computer, sending commands from one (the client) to the other (server). 在这两种情况下,我都可以从另一台计算机上的Python控制台与Python服务器通信,将命令从一个(客户端)发送到另一个(服务器)。 In all of the examples, I've had to install the libraries mentioned above on both the client and the server machines in order to facilitate the TCP communication. 在所有示例中,我都必须在客户端和服务器计算机上都安装上述库,以便于TCP通信。

So the background to my situation and question is, when does JSON enter the mix? 因此,我的情况和问题的背景是,JSON什么时候进入混合状态? This is what I want to do: 这就是我想做的:

Setup a Python TCP server that accepts a JSON string from a remote client inside (or outside) the network. 设置一个Python TCP服务器,该服务器从网络内部(或外部)的远程客户端接受JSON字符串。 The server parses the JSON string, fetches the method and parameters from the objectified string, and executes the method. 服务器解析JSON字符串,从目标字符串中获取方法和参数,然后执行该方法。 The server then sends a JSON string result to the calling client. 然后,服务器将JSON字符串结果发送到调用客户端。 In this case, the client is a mobile application (iPad, Android, etc) with a JavaScript library that I'll use to send the requests to the server. 在这种情况下,客户端是带有JavaScript库的移动应用程序(iPad,Android等),我将使用该库将请求发送到服务器。

Why would I need a Python client? 为什么需要Python客户端? From what I can gather, the client just needs to open a connection to the server and then send the JSON string, right? 据我所知,客户端只需要打开与服务器的连接,然后发送JSON字符串,对吗? Why do all the code samples include Python client examples? 为什么所有代码​​示例都包含Python客户端示例? Are they assuming a server machine is going to talk to a server machine, so they have included client code to help generate the JSON string that will be sent to the server? 他们是否假设服务器计算机将要与服务器计算机进行通信,所以他们包含了客户端代码以帮助生成将发送到服务器的JSON字符串?

If I assume that a Python client isn't really needed for anything, I've been sending JSON strings to the Python server from the iPad, but in each case the server is reporting a "Bad request syntax" error. 如果我假设确实不需要Python客户端,那么我一直在从iPad向JSON服务器发送JSON字符串,但是在每种情况下,服务器都报告“错误的请求语法”错误。 I'll pen a new question on that issue if I'm understanding the current question correctly. 如果我正确理解当前问题,我将在该问题上提出一个新问题。

Insight is appreciated. 洞察力受到赞赏。

The JSON encoding is the lingua franca of your RPC protocol, so you can indeed use any client you like. JSON编码是RPC协议的通用语言,因此您确实可以使用任何喜欢的客户端。 The implementations you found for JSON-RPC use the HTTP protocol, a very specific communication protocol built on top of TCP/IP, but you can implement the same protocol over raw TCP-IP sockets if so required. 您为JSON-RPC找到的实现使用HTTP协议,HTTP协议是基于TCP / IP构建的非常特定的通信协议,但是如果需要,您可以在原始TCP-IP套接字上实现相同的协议。

The examples include both the Python client and the server because they illustrate how to implement the JSON-RPC standard in Python, not in JavaScript or C or Lisp. 这些示例包括Python客户端服务器,因为它们说明了如何在Python中而不是JavaScript或C或Lisp中实现JSON-RPC标准。 They focus on the implementation in one language. 他们专注于一种语言的实现。 The JSON-RPC standard however, is language agnostic. 但是,JSON-RPC标准与语言无关。 It doesn't matter what language you write either the server or the client in, as long as they use the same standard. 只要服务器或客户端使用相同的标准,无论用哪种语言编写都无所谓。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM