Python JSON-RPC_2.0 TCP Server Client Explained - python

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. 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:
https://github.com/joshmarshall/jsonrpclib
http://code.activestate.com/recipes/552751-json-rpc-server-and-client/
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). 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.
So the background to my situation and question is, when does JSON enter the mix? 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. The server parses the JSON string, fetches the method and parameters from the objectified string, and executes the method. The server then sends a JSON string result to the calling client. 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.
Why would I need a Python client? From what I can gather, the client just needs to open a connection to the server and then send the JSON string, right? Why do all the code samples include Python client examples? 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?
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. 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. 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.
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. They focus on the implementation in one language. The JSON-RPC standard however, is language agnostic. It doesn't matter what language you write either the server or the client in, as long as they use the same standard.

Related

the equivalent of python socket.send() in kdb

We are using symphony in our company and I am trying to send a message to the alert bot in symphony.
Someone sent me a small python script which does this already which uses the socket library.
They send the message as socket.send(msg) using import socket in their script.
Question is : what is socket.send comparable with in kdb ? It's not a http post so it's not the .Q.hp .. Is this similar -> {h:hopen hsym`$"host:port";h"someMessageCompatibleWithSymbphonyBot";hclose h}
UPDATE: I have been told that my kdb message is not pure tcp. Can anyone point me in the right direction?
hopen works for kdb-to-kdb, not kdb-to-other so yes in that sense it's not pure tcp.
Normally when kdb needs to communicate with another system by tcp you would use some sort of middleman library to handle the communication layer.
In theory you could use the python script/package in your kdb instance if you use one of the numerous kdb<>python interfaces (pyq, embedpy, qpython etc)

How to send a request by a private protocol with Python

I want to send a request to a server which is a private protocol based on TCP (Not HTTP), How Can I send a request using Python?
Python sockets are what you are looking for. Take a look at the Python Socket class at https://docs.python.org/2/library/socket.html
This site includes examples of how to set up a server and client and provides a basic example of the fundamental TCP Communication using Python. If you need something with more control you may want to look at Scapy: http://www.secdev.org/projects/scapy/
Scapy is a python implementation that provides a framework to control almost all aspects to Network Communication all the way down to Layer 2 (Ethernet Frame).

Using Python Twisted Web HTTPChannel vs Resource

I have a specific protocol specification I am writing to that utilizes a Station to Station Protocol for authentication. Thus far I've been using Twisted web and the Resource class along with render_GET, render_POST, but once the authentication step is complete with my client the communication is encrypted, even at the HTTP protocol level. That is I lose visibility to GET /resource and only receive encrypted bytes on the TCP session. I tried looking at intercepting the render method but it is not called if there is no HTTP method parsed. Is it possible to use Resource still or do I need to "downshift" to using HTTPChannel and its ability to hand me raw data?

How would I handle multiple sockets and send data between them in Python 2.7.3?

I am trying to create a server in Python 2.7.3 which sends data to all client connections whenever one client connection sends data to the server. For instance, if client c3 sent "Hello, world!" to my server, I would like to then have my server send "Hello, world!" to client connections c1 and c2. By client connections, I mean the communications sockets returned by socket.accept(). Note that I have tried using the asyncore and twisted modules, but AFAIK they do not support this. Does anybody know any way to accomplish this?
EDIT: I have seen Twisted, but I would much rather use the socket module. Is there a way (possibly multithreading, possibly using select) that I can do this using the socket module?
You can absolutely do this using Twisted Python. You just accept the connections and set up your own handling logic (of course the library does not including built-in support for your particular communication pattern exactly, but you can't expect that).

Callback Functions across computers?

I'm writing a python server/client app. If I serialize a function on the client and pass it to the server, can the server use it as a callback? I'm assuming there must be something extra I'd have to do as the client and server are communicating via packets, I just don't know what.
What I actually need is for the server to change one of the client's attributes (when the server is ready to accept another command), and I want an alternative to having the client continuously poll the server. Thanks for any help.
Take a look at Twisted JSON RPC.
A recent SO post: Python Twisted JSON RPC

Categories